Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paginator factory in dbmapper #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"zendframework/zend-modulemanager": "~2.1",
"zendframework/zend-mvc": "~2.1",
"zendframework/zend-servicemanager": "~2.1",
"zendframework/zend-stdlib": "~2.1"
"zendframework/zend-stdlib": "~2.1",
"zendframework/zend-paginator": "~2.1"
},
"autoload": {
"psr-0": {
Expand Down
33 changes: 29 additions & 4 deletions src/ZfcBase/Mapper/AbstractDbMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\TableIdentifier;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Stdlib\Hydrator\HydratorInterface;
use Zend\Stdlib\Hydrator\ClassMethods;
use ZfcBase\EventManager\EventProvider;
Expand Down Expand Up @@ -108,12 +109,9 @@ protected function getSelect($table = null)
*/
protected function select(Select $select, $entityPrototype = null, HydratorInterface $hydrator = null)
{
$this->initialize();

$stmt = $this->getSlaveSql()->prepareStatementForSqlObject($select);

$resultSet = new HydratingResultSet($hydrator ?: $this->getHydrator(),
$entityPrototype ?: $this->getEntityPrototype());
$resultSet = $this->getResultSetPrototype($entityPrototype, $hydrator);

$resultSet->initialize($stmt->execute());
return $resultSet;
Expand Down Expand Up @@ -336,4 +334,31 @@ protected function entityToArray($entity, HydratorInterface $hydrator = null)
}
throw new Exception\InvalidArgumentException('Entity passed to db mapper should be an array or object.');
}

/**
* @param object|null $entityPrototype
* @param HydratorInterface|null $hydrator
* @return HydratingResultSet
*/
public function getResultSetPrototype($entityPrototype = null, HydratorInterface $hydrator = null)
{
$this->initialize();

$resultSet = new HydratingResultSet($hydrator ?: $this->getHydrator(),
$entityPrototype ?: $this->getEntityPrototype());

return $resultSet;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@snapshotpl

Readability could be greatly improved, e.g.

return new HydratingResultSet(
    $hydrator ?: $this->getHydrator(),
    $entityPrototype ?: $this->getEntityPrototype()
);

}

/**
* @param \Zend\Db\Sql\Select $select
* @return \Zend\Paginator\Adapter\DbSelect
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@snapshotpl

As far as I'm concerned, FQCNs should be avoided.

*/
protected function getPaginatorAdapter(Select $select)
{
$adapter = $this->getDbAdapter();
$resultSet = $this->getResultSetPrototype();

return new DbSelect($select, $adapter, $resultSet);
}
}