I'm trying to create an easy page on cakephp 3 using postgreSQL. I do not understand where is the problem: I create a table "menus". When I add a new instance (add.cpt) the parent field is empty so I cant add a parent. It's all generated from command "cake bake all menus".
View
<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>
Model
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'
]);
}
Controller
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']);
}
Table menus
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
)
Either rename the view variable
$parentMenus
to$parents
or in the view changeto