多模型上的 YII 交易

Dhi*_*sht 1 php yii

多模型上的 YII 交易你能提供我多模型上的 yii 交易的例子,比如用户模型和发布模型

ham*_*med 6

这是将事务与模型一起使用的示例:

$transaction = Yii::app()->db->beginTransaction();
try
{
    $post= new Post;
    //set attributes
    $post->save();
    $user = new User;
    //set attributes;
    $user->save();
    $transaction->commit();
 }
 catch(Exception $e)
 {
    $transaction->rollBack();
 }
Run Code Online (Sandbox Code Playgroud)

当您使用事务时,try 块中的所有代码都被视为一个事务,如果每条记录的保存因任何原因失败,事务将回滚。有关事务和 Active Record 的更多信息,您可以点击此链接