2017-10-08 141 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