findByExample in Doctrine

后端 未结 3 957
醉话见心
醉话见心 2021-02-19 03:01

Is there a method in Doctrine like Hibernate\'s findByExample method?

thanks

3条回答
  •  青春惊慌失措
    2021-02-19 04:01

    Yes.

    Let's say you have a model called Users. You have the following two classes

    abstract class Base_User extends Doctrine_Record 
    {
       //define table, columns, etc
    }
    
    class User extends Base_User
    {
    
    }
    

    in 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"));
    

提交回复
热议问题