2013-03-19 122 views
0

我有两个相关的模型,酒吧,其中酒吧$belongsTo。现在每个Foo必须具有,即两者必须同时创建。使用CakePHP的建模系统,是否可以在创建Foo(可能通过saveAll())时自动创建Bar或者我应该将该功能写入Foo::afterSave()

感谢您的帮助。

回答

0

是的 - 只是使用建立您的数据为Foo和酒吧,然后saveAll()

0

添加到Dave的答案。

如果您的数据库表支持事务,通常在MySql中为InnoDB storage engine。您还可以告诉Cake尝试一次保存两条记录。如果一个失败了,另一个被恢复。你需要通过array('atomic' => true)作为第二个参数saveAll()

$this->ModelName->saveAll($this->request->data, array("atomic" => true)); 

CookBook

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.