Roave / RoaveDbCriteria

Use Doctrine Collections expressions with Zend\Db\Sql for smart criteria / filtering / query building.
MIT License
6 stars 2 forks source link

RoaveDbCriteria

Build Status Scrutinizer Code Quality Code Coverage

v0.0.1

Example Usage

<?php
use Roave\DbCriteria\QueryExpressionVisitor;
use Doctrine\Common\Collections\Criteria;

class ContactMapper
{
    protected $visitor;
    protected $tableGateway;

    public function find(Criteria $citeria)
    {
        $select = $this->tableGateway->getSql()->select()
                       // You can apply your own Zend\Db\Sql query conditions
                       ->where(array('user_id' => $this->activeUser->id));

        // Then apply the criteria to the query
        $where  = $this->visitor->dispatch($criteria->getWhereExpression());
        $select->where($where);

        // Finally, apply limit, offset, order, and execute the select query
        QueryExpressionVisitor::apply($select, $criteria);

        return $this->tableGateway->selectWith($select);
    }
}
<?php
use Doctrine\Common\Collections\Criteria;

$criteria = Criteria::create()
                ->where(Criteria::expr()->eq('first_name', 'Evan'))
                ->andWhere(Criteria::expr()->eq('last_name', 'Coury'));

$contacts = $contactMapper->find($criteria);

In practice, you can create domain-specific criteria objects and plenty of other cool things.