该Controller
命令不会为视图设置架构或其列,您必须通过modelObj
模板内部的表对象 ( ) 自行获取:
{% set schema = modelObj.getSchema() %}
{% set fields = schema.columns() %}
{% set fields = Bake.filterFields(fields, schema, modelObj) %}
或者为事件使用事件监听器,该监听器Bake.beforeRender.Controller.controller
在控制器模板被渲染之前被调度,并允许您访问视图及其变量:
// in config/bootstrap_cli.php
use Cake\Event\Event;
use Cake\Event\EventManager;
EventManager::instance()->on(
'Bake.beforeRender.Controller.controller',
function (Event $event) {
/** @var \Cake\View\View $view */
$view = $event->getSubject();
/** @var \Cake\ORM\Table $table */
$table = $view->get('modelObj');
$schema = $table->getSchema();
$fields = $schema->columns();
$view->set(compact('schema', 'fields'));
}
);
也可以看看