cakePHP数据插入但应在编辑案例中更新

时间:2014-07-11 09:59:52

标签: php mysql cakephp

我想在Edit case中将编辑过的数据保存在两个表中。

表1:equipment_types - >戴上设备类型

表2:equipment_types_sizes - >保存每种设备类型的尺寸。

问题:表2中的数据device_types_sizes再次插入,但应在编辑案例中更新。

enter image description here

表单HTML

<input id="EquipmentTypeSize0" class="form-control valid" type="text" name="data[Sizes][0][sizes]" value="4">

<input id="EquipmentTypeSize1" class="form-control valid" type="text" name="data[Sizes][1][sizes]" value="3">

<input id="EquipmentTypeSize2" class="form-control valid" type="text" name="data[Sizes][2][sizes]" value="1">

EquipmentsController.php

if ($this->request->is('post')) {
            $this->EquipmentType->set($this->request->data);
            if ($this->EquipmentType->validates()) {
                $this->EquipmentType->id = $id;
                if ($this->EquipmentType->saveAll($this->request->data)) {
                    $this->Session->setFlash('Equipment Type has been updated successfully', 'default', 'success');
                    $this->redirect(array('controller' => 'equipments', 'action' => 'listequipmenttypes', 'admin' => true));
                    exit;
                }

            }
}

生成数组

Array
(
    [EquipmentType] => Array
        (
            [id] => 58
            [name] => Test type 3
            [description] => dadaddas
        )

    [Sizes] => Array
        (
            [0] => Array
                (
                    [sizes] => 4
                )

            [1] => Array
                (
                    [sizes] => 1
                )

            [2] => Array
                (
                    [sizes] => 2
                )
        )
)

EquipmentType Model

class EquipmentType extends AppModel {
    public $useTable = 'equipment_types';
    var $name = 'EquipmentType';
    public $cacheQueries = false;

    public $hasMany = array(
        'Sizes' => array(
            'className' => 'EquipmentTypesSize'
        )
    );        

    public $belongsTo = array(
        'AddedBy' => array(
            'className' => 'User',
            'foreignKey' => 'added_by',
            'fields' => array('first_name', 'last_name', 'email')
        ),
        'ModifiedBy' => array(
            'className' => 'User',
            'foreignKey' => 'modified_by', 
            'fields' => array('first_name', 'last_name', 'email')
        )
    );


}

EquipmentTypesSizes模型

class EquipmentTypesSizes extends AppModel {
    public $useTable = 'equipment_types_sizes';
    var $name = 'EquipmentTypesSizes';
    public $cacheQueries = false;
    public $belongsTo = array(
        'EquipmentType' => array(
            'className' => 'EquipmentType',
            'foreignKey' => 'equipment_type_id',
            'fields' => array('id', 'sizes')
        ),
        'AddedBy' => array(
            'className' => 'User',
            'foreignKey' => 'added_by',
            'fields' => array('first_name', 'last_name', 'email')
        ),
        'ModifiedBy' => array(
            'className' => 'User',
            'foreignKey' => 'modified_by', 
            'fields' => array('first_name', 'last_name', 'email')
        )
    );

    public $validate = array(
        'name' => array(
            'rule1' => array(
                'rule' => 'notEmpty',
                'message' => 'Please enter Equipment Type.'
            ),
        ),
        'description' => array(
            'rule1' => array(
                'rule' => 'notEmpty',
                'message' => 'Please enter Equipment description.'
            ),
        )
    );
}

1 个答案:

答案 0 :(得分:0)

使用saveAll 要在多个表中保存数据

也不要忘记在模型中写出正确的associations