简体   繁体   中英

Propel ORM find by multiple conditions

I need to put multiple where conditions in my propel query. What is the best way to do it. For now I'm doing it like this:

 $employee = EmployeeQuery::create()
            ->where("password = '" . $password . "' AND username = '" . $username . "'")
            ->find();

Use The filter methods based on the names of the columns in the schema

The documentation will help thoroughly understand it.

Schema:

<table name="users" phpName="User" >
   <column name="user_id" phpName="Id" />
   <column name="user_name" phpName="Name" />
   <column name="user_password" phpName="Password" />
   <column name="user_address_city" phpName="City" />
</table>

Query:

$User = UserQuery::create()
             ->filterByName($user['name'])
             ->filterByPassword($user['pass'])
             ->filterById($user['id'])
             ->find();

Keep in mind you can also use ->find() similarly, but it's a terminator and will return and object collection

$User = UserQuery::create()->findByName($user['name']); 

More useful for

$User = UserQuery::create()->findById(132); 
$User = UserQuery::create()->findPk(132);

FindOne() will also behave like this but will not return a collection, even if there are more than one results with the criteria.

$User = UserQuery::create()->findOneByName($user['name']); 

Or a combination

$u = ClientQuery::create()
                ->filterByName('John')
                ->findByCity('Orlando');

As far as maintainable code goes, you should probably not use the last example. Use the first one. It will help with readability.

hope it helps!

Use the filterByXXX functions:

$employee = EmployeeQuery::create()
           ->filterByUsername($username)
           ->filterByPassword($password)
           ->find();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM