【问题标题】:Save cakephp 2.0 associations保存 cakephp 2.0 关联
【发布时间】: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


    【解决方案1】:

    有几种方法可以解决您的问题。如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 关联,因此实体可以有多个地址。因此,您必须在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
    ));
    

    最好的,变量

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 2015-06-21
      • 2014-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多