Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Database Transactions
  • I use MySQL and 90% of the time InnoDB where transactional or row level locking tables is required for mission critical apps. I noticed that Fuel does not have transactional support at this time, so I wanted to contribute the code (Database_Transactional). It is not complete yet, but it is functional. The source can be found at https://github.com/five07/Fuel-Addons. This class does require either my latest MySQLi driver (https://github.com/five07/Fuel-Addons/tree/master/Database_MySQLi) or you can adapt a the Fuel MySQL database driver to add the transactional requirements. It is fairly easy to do. Installation: I installed my version at the app level and make sure that if you do not add it to fuel/core/classes, you add it in your app/bootstrap.php
    Fuel\Core\Autoloader::add_classes(array(
     // Add classes you want to override here
     'Fuel\\Core\\Database_Mysqli'     => APPPATH.'classes/core/database/mysqli.php',
     'Fuel\\Core\\Database_MySQLi_Result' => APPPATH.'classes/core/database/mysqli/result.php',
     'Fuel\\Core\\Database_Transaction'  => APPPATH.'classes/core/database/transaction.php',
    ));
    

    Usage: This wraps all of your dependent queries in a transaction, and on complete(), it will either commit or rollback automatically. The status() method returns true if the queries were successful, false on errors.
    $trans = Database_Transaction::instance();
    
    $trans->start();
    DB::insert('some_table')->set(array('column_name' => 'value'))->execute();
    DB::insert('some_other_table')->set(array('column_name' => 'value'))->execute();
    DB::insert('yet_another_table')->set(array('column_name' => 'value'))->execute();
    $trans->complete();
    
    if ($trans->status() !== true) {
     // Handle your errors...
     Debug::dump($trans->errors());
    }
    
    Fuel Team, once it is robust enough, feel free to add or adapt it into the core. I namespaced the class \Fuel\Core, so if it does get added, this will make upgrading easier for those already using it. If you would prefer that I do not use the Fuel namespace for addons that I wish to contribute to Fuel core functionality, please let me know. Michael
  • Hey, looking good so far! Do you think you could make this work in a more SQL engine agnostic fashion? This will only work on MySQL which is going to be an issue down the line. Also, have you considered making these changes as a fork of Fuel? You can make a branch for each addon/change/feature then we can discuss them and get them merged in much easier :)
  • Phil, I only wrote it for MySQL (using my MySQLi driver) since that is what I use 98% of the time. I do plan on porting the base functionality from my the MySQLi driver to the Fuel\MySQL driver soon. I currently have a fork for Fuel, which I have contributed a few bits already. I have been a bit confused about the direction Database & ActiveRecord are going. I notice ActiveRecord uses DB right now, but I read some discussion on PHPActiveRecord (http://fuelphp.com/forums/posts/view_reply/212), which indicates a rewritten AR package. The package Jelmer offers uses PDO, which already has transactions built in. Once the direction of Database and ActiveRecord gets finalized, I plan on completing this class and sending a pull request. I would never request a merge on any code I write until I am 100% sure that is production ready. Thanks for the feedback! Michael
  • five07,
    I have been a bit confused about the direction Database & ActiveRecord are going. I notice ActiveRecord uses DB right now, but I read some discussion on PHPActiveRecord (http://fuelphp.com/forums/posts/view_reply/212), which indicates a rewritten AR package. The package Jelmer offers uses PDO, which already has transactions built in.
    I only "offer" it in the way that I made it work within Fuel and added it to the Github account Oil uses to install packages. It uses an existing AR (php.activerecord) which will not become Fuel's default.
    Once the direction of Database and ActiveRecord gets finalized, I plan on completing this class and sending a pull request. I would never request a merge on any code I write until I am 100% sure that is production ready.
    The QueryBuilder will remain as is. Luke Baker's AR will probably be replaced with the package ORM we're currently writing from the ground up. The main reason for the latter is that our current AR was designed to be used by convention, while the Fuel philosophy is to work by configuration and only have convention for default values. Also there are design flaws in it when it comes to object hydration with many-many relationships.
  • Perfect, thanks for the clarification. I will continue to focus my efforts based on \Fuel\Core\Database. When I am happy with my code, I send a pull request from my fork. Michael

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion