Cakephp 3 Transaction management on multiple model operation

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Cakephp 3 Transaction management on multiple model operation

Ahammad Karim
$saveStatus = 1;
$conn = ConnectionManager::get('default');
$conn->begin();
$tableArticles = TableRegistry::get('Articles');
$tableUsers = TableRegistry::get('Users');
$articlesEntity = $tableArticles->newEntities($categoriesArray);
foreach ($articlesEntity as $entity) {          
    if(!$tableArticles->save($entity))
    {
        $saveStatus = 0;
    }
}
$usersEntity = $tableUsers->newEntities($usersArray);
foreach ($usersEntity as $entity) {
    if(!$tableUsers->save($entity))
    {
        $saveStatus = 0;
    }
}
if($saveStatus ==1)
{
    $conn->commit();

}
else
{
    $conn->rollback();         

}

But the transaction works for only one table.I want multiple table operation should be in transaction.

In cake 2, we do transaction using begin() and commit() on each model data source like following.

$mealOptionDataSource = $this->MealOptionFood->getDataSource ();
$foodExtrasDataSource = $this->FoodExtras->getDataSource ();
$complexOptionDataSource = $this->ComplexOptionFood->getDataSource ();

$mealOptionDataSource->begin ();
$foodExtrasDataSource->begin ();
--- Code here---
$mealOptionDataSource->commit ();
$foodExtrasDataSource->commit ();
$complexOptionDataSource->commit ();

I can't find a way how to do it in cake 3.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.