diff --git a/module/ZfModule/src/ZfModule/Mapper/Module.php b/module/ZfModule/src/ZfModule/Mapper/Module.php index 03bc181e..ca304bdc 100644 --- a/module/ZfModule/src/ZfModule/Mapper/Module.php +++ b/module/ZfModule/src/ZfModule/Mapper/Module.php @@ -2,15 +2,25 @@ namespace ZfModule\Mapper; -use ZfcBase\Mapper\AbstractDbMapper; -use Zend\Stdlib\Hydrator\HydratorInterface; use Zend\Db\ResultSet\HydratingResultSet; use Zend\Db\Sql\Expression; +use Zend\Paginator\Adapter\DbSelect; +use Zend\Paginator\Paginator; +use Zend\Stdlib\Hydrator\HydratorInterface; +use ZfcBase\Mapper\AbstractDbMapper; class Module extends AbstractDbMapper implements ModuleInterface { - protected $tableName = 'module'; + protected $tableName = 'module'; + /** + * @param int $page + * @param int $limit + * @param string $query + * @param string $orderBy + * @param string $sort + * @return Paginator + */ public function pagination($page, $limit, $query = null, $orderBy = null, $sort = 'ASC') { $select = $this->getSelect(); @@ -21,16 +31,14 @@ public function pagination($page, $limit, $query = null, $orderBy = null, $sort if (null !== $query) { $spec = function ($where) use ($query) { - $where->like('name', '%'.$query.'%')->or->like('description', '%'.$query.'%'); + $where->like('name', '%' . $query . '%')->or->like('description', '%' . $query . '%'); }; $select->where($spec); } - $adapter = new \Zend\Paginator\Adapter\DbSelect( - $select, - $this->getSql(), - new HydratingResultSet($this->getHydrator(), $this->getEntityPrototype()) - ); - $paginator = new \Zend\Paginator\Paginator($adapter); + $resultSet = new HydratingResultSet($this->getHydrator(), $this->getEntityPrototype()); + + $adapter = new DbSelect($select, $this->getSql(), $resultSet); + $paginator = new Paginator($adapter); $paginator->setCurrentPageNumber($page); $paginator->setItemCountPerPage($limit); @@ -52,6 +60,7 @@ public function findAll($limit = null, $orderBy = null, $sort = 'ASC') $entity = $this->select($select); $this->getEventManager()->trigger('find', $this, ['entity' => $entity]); + return $entity; } @@ -68,12 +77,13 @@ public function findByLike($query, $limit = null, $orderBy = null, $sort = 'ASC' } $spec = function ($where) use ($query) { - $where->like('name', '%'.$query.'%')->or->like('description', '%'.$query.'%'); + $where->like('name', '%' . $query . '%')->or->like('description', '%' . $query . '%'); }; $select->where($spec); $entity = $this->select($select); $this->getEventManager()->trigger('find', $this, ['entity' => $entity]); + return $entity; } @@ -94,36 +104,33 @@ public function findByOwner($owner, $limit = null, $orderBy = null, $sort = 'ASC $entity = $this->select($select); $this->getEventManager()->trigger('find', $this, ['entity' => $entity]); + return $entity; } public function findByName($name) { - $select = $this->getSelect() - ->where(['name' => $name]); - - $entity = $this->select($select)->current(); - $this->getEventManager()->trigger('find', $this, ['entity' => $entity]); - return $entity; + return $this->findBy('name', $name); } public function findByUrl($url) { - $select = $this->getSelect() - ->where(['url' => $url]); - - $entity = $this->select($select)->current(); - $this->getEventManager()->trigger('find', $this, ['entity' => $entity]); - return $entity; + return $this->findBy('url', $url); } public function findById($id) { - $select = $this->getSelect() - ->where(['module_id' => $id]); + return $this->findBy('module_id', $id); + } + + public function findBy($key, $value) + { + $select = $this->getSelect(); + $select->where([$key => $value]); $entity = $this->select($select)->current(); $this->getEventManager()->trigger('find', $this, ['entity' => $entity]); + return $entity; } @@ -131,24 +138,24 @@ public function insert($entity, $tableName = null, HydratorInterface $hydrator = { $result = parent::insert($entity, $tableName, $hydrator); $entity->setId($result->getGeneratedValue()); + return $result; } public function update($entity, $where = null, $tableName = null, HydratorInterface $hydrator = null) { if (!$where) { - $where = 'module_id = ' . $entity->getId(); + $where = ['module_id' => $entity->getId()]; } - return parent::update($entity, $where, $tableName, $hydrator); } public function delete($entity, $where = null, $tableName = null) { if (!$where) { - $where = 'module_id = ' . $entity->getId(); + $where = ['module_id' => $entity->getId()]; } - return parent::delete($where, $tableName); + return parent::delete($where, $tableName); } public function getTotal() @@ -157,8 +164,8 @@ public function getTotal() $select->columns(['num' => new Expression('COUNT(*)')]); $stmt = $this->getSlaveSql()->prepareStatementForSqlObject($select); - $row = $stmt->execute()->current(); + return $row['num']; } }