- Getting started
- Basics
- Finding
- Relation Mapping
- Models
- Database
- Schema
- Multiple databases
- Transactions
- Nuke
- BeanCan
- Advanced
- Architecture
- Other
Transactions
RedBeanPHP offers three simple methods to use database transactions: begin(), commit() and rollback(). A transaction is a unit of work performed within a database management system (or similar system) against a database, and treated in a coherent and reliable way independent of other transactions. To begin a transaction use R::begin(), to commit all changes to the database use R::commit() and finally to rollback all pending changes and make sure the database is left untouched use R::rollback(). Usage:
R::begin();
try{
R::store($page);
R::commit();
}
catch(Exception $e) {
R::rollback();
}
The RedBeanPHP transactional system works exactly like conventional database transactions. Because RedBeanPHP throws exceptions, you can catch the exceptions thrown by methods like R::store(), R::delete(), R::associate() etc, and perform a rollback(). The rollback() will completely undo all the pending database changes.
If you are new to transactions, consider reading about database transactions first.
Many databases automatically commit after changing schemas, so make sure you test your transactions after R::freeze(true); !
Tweet
User contributed notes. Please use the comment section to provide tips, notes and examples. To ask for support or to provide feedback use the forum. For bug reports use Github Issue Tracker.
Site comments powered by Disqus