保存 cakephp 2.0 关联
Save cakephp 2.0 associations
我试图在 Cakephp 2.0 中保存相关数据,在数据库中我有两个 tables,table 实体(id,main_name)和地址(id,entity_id, 城市)
在实体模型中我建立了关联:
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没有关联,也没有保存地址中的城市table,我还需要做其他的吗控制器?
有多种方法可以解决您的问题。就像 CakeBook: Saving your data you can use saveAssociated() or Save each Model step by step.
中描述的那样
使用 saveAssociated() 保存
在你的Controller/EntitiesController.php
中:
public function add() {
if ($this->request->is('post')) {
$this->Entity->create();
// Use saveAssociated() instead of save() here
if ($this->Entity->saveAssociated($this->request->data)) {
$this->Flash->success(__('The entity has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Flash->error(__('The entity could not be saved. Please, try again.'));
}
}
$addresses = $this->Entity->Address->find('all');
$this->set($addresses);
}
在你的View/Entities/add.ctp
中:
<?php
echo $this->Form->input('main_name', array(
'type' => 'text',
'class' => 'form-control',
'label' => false
));
// Make sure you use Address.0.city
echo $this->Form->input('Address.0.city', array(
'type' => 'text',
'class' => 'form-control',
'label' => false
));
?>
由于您使用了 hasMany 关联,一个实体可以有多个地址。为此,您必须在 Address.0.city
中设置 0
。这将导致这样的数据数组:
array(
'Entity' => array(
'main_name' => 'Fancy Name'
),
'Address' => array(
(int) 0 => array(
'city' => 'Cool City'
)
)
)
逐步保存模型
另一种方法是保存实体,然后使用 entity_id 保存地址,如 CakeBook:
中所述
在你的Controller/EntitiesController.php
中:
public function add() {
if (!empty($this->request->data)) {
// save Entity
$entity = $this->Entity->save($this->request->data);
if (!empty($entity)) {
// Set the EntityId to the data array and save the Address with the EntityId
$this->request->data['Address']['entity_id'] = $this->Entity->id;
$this->Entity->Address->save($this->request->data);
}
}
}
在这种情况下,您的 View/Entities/add.ctp
地址表单将如下所示:
echo $this->Form->input('Address.city', array(
'type' => 'text',
'class' => 'form-control',
'label' => false
));
最佳,变量
我试图在 Cakephp 2.0 中保存相关数据,在数据库中我有两个 tables,table 实体(id,main_name)和地址(id,entity_id, 城市)
在实体模型中我建立了关联:
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没有关联,也没有保存地址中的城市table,我还需要做其他的吗控制器?
有多种方法可以解决您的问题。就像 CakeBook: Saving your data you can use saveAssociated() or Save each Model step by step.
中描述的那样使用 saveAssociated() 保存
在你的Controller/EntitiesController.php
中:
public function add() {
if ($this->request->is('post')) {
$this->Entity->create();
// Use saveAssociated() instead of save() here
if ($this->Entity->saveAssociated($this->request->data)) {
$this->Flash->success(__('The entity has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Flash->error(__('The entity could not be saved. Please, try again.'));
}
}
$addresses = $this->Entity->Address->find('all');
$this->set($addresses);
}
在你的View/Entities/add.ctp
中:
<?php
echo $this->Form->input('main_name', array(
'type' => 'text',
'class' => 'form-control',
'label' => false
));
// Make sure you use Address.0.city
echo $this->Form->input('Address.0.city', array(
'type' => 'text',
'class' => 'form-control',
'label' => false
));
?>
由于您使用了 hasMany 关联,一个实体可以有多个地址。为此,您必须在 Address.0.city
中设置 0
。这将导致这样的数据数组:
array(
'Entity' => array(
'main_name' => 'Fancy Name'
),
'Address' => array(
(int) 0 => array(
'city' => 'Cool City'
)
)
)
逐步保存模型
另一种方法是保存实体,然后使用 entity_id 保存地址,如 CakeBook:
中所述在你的Controller/EntitiesController.php
中:
public function add() {
if (!empty($this->request->data)) {
// save Entity
$entity = $this->Entity->save($this->request->data);
if (!empty($entity)) {
// Set the EntityId to the data array and save the Address with the EntityId
$this->request->data['Address']['entity_id'] = $this->Entity->id;
$this->Entity->Address->save($this->request->data);
}
}
}
在这种情况下,您的 View/Entities/add.ctp
地址表单将如下所示:
echo $this->Form->input('Address.city', array(
'type' => 'text',
'class' => 'form-control',
'label' => false
));
最佳,变量