diff --git a/lib/Doctrine/Common/Collections/Criteria.php b/lib/Doctrine/Common/Collections/Criteria.php index 3f9d43a82..fabe92a4f 100644 --- a/lib/Doctrine/Common/Collections/Criteria.php +++ b/lib/Doctrine/Common/Collections/Criteria.php @@ -75,6 +75,25 @@ public static function create() return new static(); } + /** + * Creates an instance with same behaviour as criteria parameters passed on ORM find methods. + * + * @param mixed[] $findCriteria Array of different keys and values. Gets the same format as argument expected on + * ObjectRepository::findBy and ObjectRepository::findOneBy methods. + * + * @return Criteria + */ + public static function fromFindCriteria(array $findCriteria) + { + $criteria = static::create(); + + foreach ($findCriteria as $key => $value) { + $criteria->andWhere(static::expr()->eq($key, $value)); + } + + return $criteria; + } + /** * Returns the expression builder. * diff --git a/tests/Doctrine/Tests/Common/Collections/CriteriaTest.php b/tests/Doctrine/Tests/Common/Collections/CriteriaTest.php index b3d3a06fd..e72b07ed1 100644 --- a/tests/Doctrine/Tests/Common/Collections/CriteriaTest.php +++ b/tests/Doctrine/Tests/Common/Collections/CriteriaTest.php @@ -15,6 +15,18 @@ public function testCreate() $this->assertInstanceOf('Doctrine\Common\Collections\Criteria', $criteria); } + public function testFromFindCriteria() + { + $criteria = Criteria::fromFindCriteria(array('name' => 'test', 'foo' => 42)); + + /** @var CompositeExpression $where */ + $where = $criteria->getWhereExpression(); + $this->assertInstanceOf('Doctrine\Common\Collections\Expr\CompositeExpression', $where); + + $this->assertSame(CompositeExpression::TYPE_AND, $where->getType()); + $this->assertCount(2, $where->getExpressionList()); + } + public function testConstructor() { $expr = new Comparison("field", "=", "value");