2017-10-08 143 views
0

我想保存在CakePHP 2.0相关的数据,在我有两个表,表的实体(ID,main_name),并不会忽略(ID,ENTITY_ID,市)保存CakePHP的2.0协会

中的实体模型数据库我所做的关联关系:

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没有关系,并且不会将城市保存在地址表中,是否必须在控制器中执行其他任何操作?

回答

0

有几种解决问题的方法。像CakeBook: Saving your data描述,您可以使用saveAssociated()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一个实体的关联可以有多个地址。因此,您必须将0设置为Address.0.city。这将导致在数据阵列是这样的:

array(
    'Entity' => array(
     'main_name' => 'Fancy Name' 
    ), 
    'Address' => array(
     (int) 0 => array(
      'city' => 'Cool City' 
     ) 
    ) 
) 

保存模型步步

另一种方法是,保存实体,然后保存该地址与像在CakeBook描述的ENTITY_ID:

在你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 
)); 

最佳,变量

+0

很好的解释谢谢 – Henrique