我可以为其他字段值设置模型记录ID吗? (没有update
)
另一方面,将此代码混合到一行或两行:
$this->Model->create($data);
$this->Model->save();
$this->Model->create($data); // create again
$this->Model->set('link', $this->Model->id); // get record id
$this->Model->save(); // update
感谢。
答案 0 :(得分:0)
是的,但您需要正确调用正确的模型方法。您还需要确保将信息正确存储在要传递的数组中以进行保存。因此,假设您要在places
表中添加记录。假设您的架构如下所示:
CREATE TABLE 'places' (
`id` CHAR(36) NOT NULL,
`reference_id` CHAR(36) NULL,
`name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`id`)
);
然后你有以下内容:
$data['Place']['name'] = 'New York';
// create the first record to get the PK ID that will
// be used in the reference_id of the next record
$this->Place->create();
$this->Place->save($data);
// set the reference ID to the ID of the previously saved record
$data['Place']['reference_id'] = $this->Place->id;
// create the next record recording the previous ID as
// the reference_id
$this->Place->create();
$this->Place->save($data);