[ACCEPTED]-findByExample in Doctrine-doctrine
You can use the findBy
method, which is inherited 11 and is present in all repositories.
Example:
$criteria = array('name' => 'someValue', 'status' => 'enabled');
$result = $em->getRepository('SomeEntity')->findBy($criteria);
You 10 can create findByExample
method in one of your repositories 9 using a definition like this:
class MyRepository extends Doctrine\ORM\EntityRepository {
public function findByExample(MyEntity $entity) {
return $this->findBy($entity->toArray());
}
}
In order for 8 this to work, you will have to create your 7 own base class for the entities, implementing 6 the toArray
method.
MyEntity
can also be an interface, which 5 your specific entities will have to implement 4 the toArray
method again.
To make this available 3 in all your repositories, ensure that you 2 are extending your base repository class 1 - in this example, the MyRepository
one.
P.S I assume you are talking about Doctrine 2.x
Yes.
Let's say you have a model called 2 Users. You have the following two classes
abstract class Base_User extends Doctrine_Record
{
//define table, columns, etc
}
class User extends Base_User
{
}
in 1 some other object you can do
$user = new User;
//This will return a Doctrine Collection of all users with first name = Travis
$user->getTable()->findByFirstName("Travis");
//The above code is actually an alias for this function call
$user->getTable()->findBy("first_name", "Travis");
//This will return a Doctrine Record for the user with id = 24
$user->getTable()->find(24);
//This will return a Doctrine Collection for all users with name=Raphael and
//type = developer
$user->getTable()
->findByDql("User.name= ? AND User.type = ?", array("Raphael", "developer"));
$users = $userTable->findByIsAdminAndIsModeratorOrIsSuperAdmin(true, true, true);
See http://www.doctrine-project.org/projects/orm/1.2/docs/manual/dql-doctrine-query-language/en
0
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.