dougong2005 2012-03-28 17:39
浏览 124
已采纳

cakePHP复杂查找查询

how do I build a find() query in cakePHP using these conditions:

Find where 
MyModel.x = 1 and MyModel.y = 2 OR 
MyModel.x = 1 and MyModel.y value does not exist (or is equal to empty string)

Can somebody tell me how I can go about building such find query?

  • 写回答

2条回答 默认 最新

  • donglian3061 2012-03-28 17:46
    关注

    I'm gonna give you some pointers, but you need to try to do this as it's very basic and it's always good to practice.

    A basic find in cake is in the form of

    $this->ModelName->find('all');
    

    This in its default form does a SELECT * from model_names (convention is to have singular ModelName for plural table name - model_names)

    To add conditions:

    $this->ModelName->find('all', array('conditions' => array('ModelName.x' => 1));
    

    To add AND conditions

    $this->ModelName->find('all', array('conditions' => array(
       'ModelName.x' => 1, 'ModelName.y' => 2
    ));
    

    To add OR conditions

    $this->ModelName->find('all', array('conditions' => array(
       'OR' => array(
          'ModelName.x' => 1, 'ModelName.y' => 2
        )
    ));
    

    To combine both

    $this->ModelName->find('all', array('conditions' => array(
       'ModelName.y is not' => null,
       'OR' => array(
          'ModelName.x' => 1, 'ModelName.y' => 2
        )
    ));
    
    // where y is not null and (x = 1 or y = 2) 
    

    http://book.cakephp.org/1.3/view/1030/Complex-Find-Conditions

    (btw I'm sure there will be users giving you the exact answers, so just take my answer for your reference :) )

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services