CakePHP 2.8 saveAll创建两个记录而不是一个记录

时间:2019-01-04 17:02:41

标签: cakephp-2.0 saving-data

我正在克隆具有相关数据的里程碑记录。我首先从源里程碑中找到数据,然后更改数组的内容并删除所有“ id”字段。然后我尝试一个“ saveAll”。所有关联数据均已正确创建,但是“里程碑”表现在具有2条新记录,而不是1条。第二条记录包含关联数据使用的ID,那么为什么第一条记录甚至存在?

数据(简化)和致电:

    $test = array(
        'Milestone' => array(
            'name' => 'Clone Test',
            'customer_status' => '',
            'time_to_complete' => '1',
            'is_active' => true,
        ),
        'MilestoneAlert' => array(
            (int) 0 => array(
                'name' => 'Clone Test',
                'type' => 'customer',
                'milestone_alert_type_id' => '0',
                'email' => null,
                'subject' => 'First alert!',
                'message' => 'Add a message',
                'recipient_name' => '',
                'is_active' => true,
            ),
            (int) 1 => array(
                'name' => 'Clone Test 1',
                'type' => 'customer',
                'milestone_alert_type_id' => '0',
                'email' => null,
                'subject' => 'Second alert!',
                'message' => 'Add a message',
                'recipient_name' => '',
                'is_active' => true,
            )
        ),
        'MilestoneCondition' => array(
            (int) 0 => array(
                'name' => 'Clone Test',
                'condition' => 'First condition.',
                'is_active' => true,
            ),
            (int) 1 => array(
                'name' => 'Clone Test 1',
                'condition' => 'Second condition.',
                'is_active' => true,
            )
        )
    );
    $this->loadModel('Milestone');
    $this->Milestone->create();
    $this->Milestone->saveAll($test);

在Model / Milestone.php中:

public $hasMany = array(
    'MilestoneAlert' => array(
        'className' => 'MilestoneAlert',
        'foreignKey' => 'milestone_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    ),
    'MilestoneCondition' => array(
        'className' => 'MilestoneCondition',
        'foreignKey' => 'milestone_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => 'MilestoneCondition.order',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )
);

在Model / MilestoneAlert.php中:

public $belongsTo = array(
    'Milestone' => array(
        'className' => 'Milestone',
        'foreignKey' => 'milestone_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

在Model / MilestoneCondition.php中:

public $belongsTo = array(
    'Milestone' => array(
        'className' => 'Milestone',
        'foreignKey' => 'milestone_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

运行该方法后,在“ milestone_conditions”中有两个记录,在“ milestone_alerts”中有两个记录,在“ milestones”中有两个相同记录。如何避免创建两个“里程碑”记录?

1 个答案:

答案 0 :(得分:0)

尝试获得像这样的原始数据:

$test= $this->Milestone->findById('insert your id');
$this->Milestone->create(); // Create a new record
$this->Milestone->saveAll($test,array('deep' => true)); // And save it