ZF2 - Database transactions with updating multiple tables -


i want use zf2 db transaction update multiple tables. transaction single table:

 $connection = null;  try {         $connection = $this->tablegateway->getadapter()->getdriver()->getconnection();         $connection->begintransaction();         $this->tablegateway->insert($data);         $connection->commit();      }   catch (exception $e) {        if ($connection instanceof \zend\db\adapter\driver\connectioninterface) {           $connection->rollback();        }   } 

now want update 2 tables inside 1 transaction. in zf1 did creating instance of table2 class , calling appropriate method inside same transaction. since don't know method call model class inside model, cannot zf1. need simple task adding new rows tbl_invoice , updating tbl_runno table's running number invoices when entering new bill(invoice).

use datamapper design pattern. pattern tablegateway serves project data single table.

  • http://akrabat.com/development/objects-in-the-model-layer
  • http://akrabat.com/php/objects-in-the-model-layer-part-2
  • http://www.slideshare.net/aaronsaray/midwest-php-2013
  • https://leanpub.com/zendframework2-en can use this:

    namespace sccontent\mapper;  use zend\db\sql\sql; use zend\db\sql\select; use zend\db\sql\sqlinterface; use zend\db\sql\preparablesqlinterface;  use zend\db\adapter\adapterinterface; use zend\db\adapter\driver\resultinterface;  use zend\stdlib\hydrator\classmethods; use zend\stdlib\hydrator\hydratorinterface;  abstract class abstractdbmapper {     /**      * @var string      */     const joininner = select::join_inner;      /**      * @var string      */     const joinleft = select::join_left;      /**      * @var string      */     const joinright = select::join_right;      /**      * @var zend\db\adapter\adapterinterface      */     protected $_adapter;      /**      * @var zend\db\sql\sqlinterface      */     protected $_sql;      /**      * @var zend\stdlib\hydratorinterface      */     protected $_hydrator;      /**      * @var array      */     protected $_tables = array();      /**      * @param zend\db\adapter\adapterinterface $adapter      */     public function setadapter(adapterinterface $adapter)     {         $this->_adapter = $adapter;     }      /**      * @return zend\db\adapter\adapterinterface      */     public function getadapter()     {         if(!$this->_adapter instanceof adapterinterface) {             throw new exception('adapter not installed.');         }         return $this->_adapter;     }      /**      * @param zend\db\sql\sqlinterface $sql      */     public function setsql(sqlinterface $sql)     {         $this->_sql = $sql;     }      /**      * @return zend\db\sql\sqlinterface      */     public function getsql()     {         if(!$this->_sql instanceof sqlinterface) {             $this->_sql = new sql($this->getadapter());         }         return $this->_sql;     }      /**      * @param zend\stdlib\hydratorinterface $hydrator      */     public function sethydrator(hydratorinterface $hydrator)     {         $this->_hydrator = $hydrator;     }      /**      * @return zend\stdlib\hydratorinterface      */     public function gethydrator()     {         if(!$this->_hydrator instanceof hydratorinterface) {             $this->_hydrator = new classmethods();         }         return $this->_hydrator;     }      /**      * @param string $alias      * @param string $name      */     public function settable($alias, $name)     {         $this->_tables[$alias] = $name;     }      /**      * @param string $alias      * @throws exception      * @return string      */     public function gettable($alias)     {         if(!array_key_exists($alias, $this->_tables)) {             throw new exception(sprintf("unknown table alias '%s'.", $alias));         }         return $this->_tables[$alias];     }      /**      * @return int|null|false      */     protected function lastinsertid()     {         return $this->adapter->getdriver()->getconnection()->getlastgeneratedvalue();     }      /**      * @param void      * @return void      */     protected function begintransaction()     {         $this->getadapter()->getdriver()->getconnection()->begintransaction();     }      /**      * @param void      * @return void      */     protected function commit()     {         $this->getadapter()->getdriver()->getconnection()->commit();     }      /**      * @return bool      */     protected function intransaction()     {         return $this->getadapter()->getdriver()             ->getconnection()->getresource()->intransaction();     }      /**      * @param void      * @return void      */     protected function rollback()     {         $this->getadapter()->getdriver()->getconnection()->rollback();     }      /**      * @param zend\db\sql\preparablesqlinterface $sqlobject      * @return zend\db\adapter\resultinterface      */     protected function execute(preparablesqlinterface $sqlobject)     {         return $this->getsql()->preparestatementforsqlobject($sqlobject)->execute();     }      /**      * @param zend\db\adapter\resultinterface $source      * @return array      */     protected function toarray(resultinterface $source)     {         $result = array();         foreach($source $item) {             $result[] = $item;         }         return $result;     }      /**      *      */     protected function tostring(sqlinterface $sqlobject)     {         return $this->getsql()->getsqlstringforsqlobject($sqlobject);     } } 

    example of use:

    <?php     namespace sccontent\mapper;  use zend\db\adapter\adapterinterface;  class contentmapper extends abstractdbmapper {     /**      * @var string      */     const contenttablealias = 'contentalias';      /**      * @var string      */     const userstablealias = 'usersalias';      /**      * @param adapterinterface $adapter      */     public function __construct(adapterinterface $adapter) {         $this->setadapter($adapter);     }      /**      * @var array      */     protected $_tables = array(         self::contenttablealias => 'sc_content',         self::userstablealias   => 'sc_users'     );      /**      * @param integer $id      * @return null | array      */     public function findbyid($id)     {         $select = $this->getsql()->select()             ->from(array('content' => $this->gettable(self::contenttablealias)))             ->join(                 array('users' => $this->gettable(self::userstablealias)),                 'content.author = users.user_id',                 array('username'),                 self::joininner             )             ->where(array('`content`.`id` = ?' => $id));         return $this->execute($select)->current();     } } 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -