【发布时间】:2017-10-08 18:10:14
【问题描述】:
我正在尝试将关联数据保存在 Cakephp 2.0 中,在数据库中我有两个表,表实体 (id, main_name) 和地址(id, entity_id, city)
在实体模型中我建立了关联:
public $hasMany = array(
'Address' => array(
'className' => 'Address',
'foreignKey' => 'entity_id'
)
);
在 AdressesController 中我保存了以下数据:
public function add() {
if ($this->request->is('post')) {
if($this->Entity->save($this->request->data)) {
$this->Flash->success('Entity successfully registered!');
$this->redirect(array('action' => 'add'));
} else {
$this->Flash->error(Oops, we could not register this entity!
Make sure it already exists.');
}
}
}
在视图中,我的表单如下所示:
<?php
echo $this->Form->input(
'Entity.main_name',
array(
'type' => 'text',
'class' => 'form-control',
'label' => false
)
);
?>
<?php
echo $this->Form->input(
'Address.city',
array(
'type' => 'text',
'class' => 'form-control',
'label' => false
)
);
?>
实体的数据正常保存在数据库中,但是没有关联entity_id,也没有把城市保存在addresses表中,是不是还要在controller中做其他事情?
【问题讨论】:
标签: php cakephp cakephp-2.0