Welcome Guest, Not a member yet? Register   Sign In
Struggling with transaction using multiple models
#1

(This post was last modified: 10-09-2020, 08:04 AM by mlurie.)

I'm struggling with how to get transactions to work when saving with multiple models.  I am developing a Property Management application.  When a single tenant property is created, the data for the property as well as a default unit is saved to the database.  Here is my code:

PHP Code:
$property_model = new PropertyModel;
$unit_model = new UnitModel;

$property_model->transStart();

$property_save = $property_model->save($property);
$unit_save = $unit_model->save($unit);

$property_model->transComplete(); 

if(!$property_save || !$unit_save) {
   //Handle validation errors
} elseif($property_model->transStatus() !== TRUE) {             
   
//Handle failed transaction
} else {
   
//Success action


When I force $unit_model->save($unit) to fail, the $property-model->save($property) is still inserted into the database.  I know I am probably using transactions incorrectly with my models, but I am not experienced enough with CodeIgniter 4 to know what I am doing wrong.
Reply


Messages In This Thread
Struggling with transaction using multiple models - by mlurie - 10-09-2020, 08:03 AM



Theme © iAndrew 2016 - Forum software by © MyBB