【问题标题】:Propel ORM find by multiple conditions通过多个条件推动 ORM 查找
【发布时间】:2015-03-30 16:20:51
【问题描述】:

我需要在推进查询中添加多个where 条件。最好的方法是什么。现在我正在这样做:

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

【问题讨论】:

    标签: php mysql database orm propel


    【解决方案1】:

    使用filterByXXX 函数:

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

    【讨论】:

      【解决方案2】:

      使用基于模式中列名的过滤方法

      The documentation 将有助于彻底理解它。

      架构:

      <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>
      

      查询:

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

      请记住,您也可以类似地使用-&gt;find(),但它是一个终止符,将返回和对象集合

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

      更有用

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

      FindOne() 的行为也是如此,但不会返回集合,即使有多个符合条件的结果。

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

      或组合

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

      就可维护代码而言,您可能不应该使用最后一个示例。使用第一个。这将有助于提高可读性。

      希望对你有帮助!

      【讨论】:

        猜你喜欢
        • 2012-08-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-15
        相关资源
        最近更新 更多