1

我想使用 ZF2 db 事务来更新多个表。通常我通过这样的方式为单个表进行事务处理:

 $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();
       }
  }

现在我想在一个事务中更新两个表。在 ZF1 中,我通过创建 table2 类的实例并在同一事务中调用其适当的方法来做到这一点。但是由于我不知道在模型中调用另一个模型类的方法,所以我不能像 ZF1 那样做。我需要这个来完成一个简单的任务,比如在输入新账单(发票)时向 tbl_invoice 添加新行并更新 tbl_runno 表的发票运行编号。

4

2 回答 2

3

在您的控制器中,您可以执行以下操作:

$db = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
$con = $db->getDriver()->getConnection();
$con->beginTransaction();
try {
    //get tables and do sth
    $con->commit();
} catch (Exception $e) {
    $con->rollback();
}
于 2014-07-23T09:32:30.000 回答
0

使用 DataMapper 设计模式。Pattern TableGateway 用于将数据投影到单个表中。

  • 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 你可以使用这样的东西:

    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 is 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 as $item) {
                $result[] = $item;
            }
            return $result;
        }
    
        /**
         *
         */
        protected function toString(SqlInterface $sqlObject)
        {
            return $this->getSql()->getSqlStringForSqlObject($sqlObject);
        }
    }
    

    使用示例:

    <?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();
        }
    }
    
于 2013-08-10T12:20:15.193 回答