【问题标题】:CakePHP - select from database with conditionCakePHP - 从有条件的数据库中选择
【发布时间】:2016-12-01 17:52:16
【问题描述】:

我有这个选择器:

$this->Table->find('list',array('contain'=>false,'conditions'=>array('status'=>1,'unit_id'=>null,'country_id'=>$countryId,'eday'=>$eday),'fields'=>'id'));

这很完美。但是现在我需要另一个我找不到如何做到这一点;)

我需要从表中选择所有记录,但条件是:

'eday'>=$eday AND 'eday'<$eday+7 

这很容易吗?也许这是个愚蠢的问题,但我在 PHP 中没有 exp ;)

【问题讨论】:

  • 对不起 [code]'eday'>=$eday AND 'eday'

标签: cakephp multiple-conditions


【解决方案1】:

在 cakephp 中,and 条件的等价物是
示例:column1 = 'value' AND column2 = 'value'

'AND' => array(
    'column1' => 'value',
    'column2' => 'value'
)

所以你的查询构建器会是这样的

$this->Table->find('list',array(
    'contain' => false,
    'conditions' => array(
        'status' => 1,
        'unit_id' => null,
        'country_id' => $country,
        'AND' => array(
            'eday >=' => $eday,
            'eday <' => ($eday+7)
        )
    ),
    'fields'=>'id'
));

【讨论】:

    【解决方案2】:

    只需在当前条件数组中传递一个 AND 数组:

    $this->Table->find('list',array('contain'=>false, 'conditions'=>array('status'=>1,'unit_id'=>null,'country_id'=>$countryId,'eday'=>$eday, AND => array( array('eday >=' => $eday) , array( 'eday <' => $eday+7) )), 'fields'=>'id' ));
    

    【讨论】:

    • 不,那是给我“发生错误”。
    • 你遇到什么样的错误??语法错误或数据库错误??
    • 错误:发生内部错误。
    • 比在主条件数组中只使用单个 AND 数组
    • 大声笑你的条件不正确AND =&gt; 没有像 cakephp 这样的条件
    【解决方案3】:

    你有没有尝试过这样的事情:

    $conditions['status'] = 1;
    $conditions['unit_id'] = null;
    $conditions['country_id'] = $countryId;
    $conditions['eday >='] = $eday;
    $conditions['eday <'] = $eday + 7;
    
    $this->Table
        ->find('list') //either use this
        ->find('all') //or use this
        ->find() //or this
        ->where($conditions)
        ->select(['addFiledsToSelectHere'])
    

    这看起来更干净。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-31
      相关资源
      最近更新 更多