我有两个相关的模型,富和酒吧,其中酒吧$belongsTo
富。现在每个Foo必须具有条,即两者必须同时创建。使用CakePHP的建模系统,是否可以在创建Foo(可能通过saveAll()
)时自动创建Bar或者我应该将该功能写入Foo::afterSave()
?
感谢您的帮助。
我有两个相关的模型,富和酒吧,其中酒吧$belongsTo
富。现在每个Foo必须具有条,即两者必须同时创建。使用CakePHP的建模系统,是否可以在创建Foo(可能通过saveAll()
)时自动创建Bar或者我应该将该功能写入Foo::afterSave()
?
感谢您的帮助。
是的 - 只是使用建立您的数据为Foo和酒吧,然后saveAll()
。
添加到Dave的答案。
如果您的数据库表支持事务,通常在MySql中为InnoDB storage engine
。您还可以告诉Cake尝试一次保存两条记录。如果一个失败了,另一个被恢复。你需要通过array('atomic' => true)
作为第二个参数saveAll()
$this->ModelName->saveAll($this->request->data, array("atomic" => true));
atomic: If true (default), will attempt to save all records in a single transaction.
Should be set to false if database/table does not support transactions.