我正在尝试使用postgreSQL在cakephp 3上创建一个简单的页面。 我不明白问题出在哪里: 我创建了一个表“菜单”。当我添加一个新实例(add.cpt)时,父字段为空,所以我无法添加父实例。 它全部来自命令“蛋糕烘烤所有菜单”。
查看
<div class="actions columns large-2 medium-3">
<h3><?= __('Actions') ?></h3>
<ul class="side-nav">
<li><?= $this->Html->link(__('List Menus'), ['action' => 'index']) ?></li>
</ul>
</div>
<div class="menus form large-10 medium-9 columns">
<?= $this->Form->create($menu); ?>
<fieldset>
<legend><?= __('Add Menu') ?></legend>
<?php
echo $this->Form->input('name');
echo $this->Form->input('parent_id');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
模型
public function initialize(array $config)
{
$this->table('menus');
$this->displayField('name');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('ParentMenus', [
'className' => 'Menus',
'foreignKey' => 'parent_id'
]);
$this->hasMany('ChildMenus', [
'className' => 'Menus',
'foreignKey' => 'parent_id'
]);
}
控制器
public function add()
{
$menu = $this->Menus->newEntity();
if ($this->request->is('post')) {
$menu = $this->Menus->patchEntity($menu, $this->request->data);
if ($this->Menus->save($menu)) {
$this->Flash->success('The menu has been saved.');
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error('The menu could not be saved. Please, try again.');
}
}
$parentMenus = $this->Menus->ParentMenus->find('list', ['limit' => 200]);
$this->set(compact('menu', 'parentMenus'));
$this->set('_serialize', ['menu']);
}
表格菜单
CREATE TABLE menus
(
id serial NOT NULL,
name character varying(255),
created timestamp with time zone,
modified time with time zone,
parent_id integer,
CONSTRAINT menus_pkey PRIMARY KEY (id),
CONSTRAINT menus_parent_id_fkey FOREIGN KEY (parent_id)
REFERENCES menus (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
答案 0 :(得分:0)
将视图变量$parentMenus
重命名为$parents
或在视图更改中
echo $this->Form->input('parent_id');
到
echo $this->Form->input('parent_id', ['options' => $parentMenus]);