Skip to content
This repository has been archived by the owner on May 1, 2019. It is now read-only.

Improve module mapper #323

Merged
merged 2 commits into from
Jan 24, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 37 additions & 30 deletions module/ZfModule/src/ZfModule/Mapper/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -94,61 +104,58 @@ 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;
}

public function insert($entity, $tableName = null, HydratorInterface $hydrator = null)
{
$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()
Expand All @@ -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'];
}
}