【问题标题】:how to write cakephp and and or query如何编写cakephp and and or 查询
【发布时间】:2015-03-05 12:41:34
【问题描述】:
 $tmp_sales    = $this->tmp_sale->find('all', array(
                'conditions' => array(
                    array(
                        'no' => $no,
                        'barcode' => $barcode,'employee' => $emp, 'store' => $store_name
                    )
                )
            ));

这是我目前的查询。

select * from tmp_sale where  no = '$no' and 
                       'employee' ='$emp' and  store = '$store_name' and ( barcode='$barcode' or name='$barcode')

我想像上面那样做出改变。如何在 cakephp 中编写这样的查询

【问题讨论】:

标签: php cakephp cakephp-2.0 cakephp-1.3 cakephp-2.3


【解决方案1】:
'conditions' => array(
    'no' => $no,
    'employee' => $emp,
    'store '=> $store_name,
    'OR' => array(
        array('barcode' => $barcode),
        array('name' => $barcode)
    )
)

【讨论】:

    【解决方案2】:

    Cakephp 模型名称应为单数,tmp_sale 模型名称应替换为 TmpSale 名称。 cakephp 的查询如下所示。 你应该试试这个

     $tmp_sales = $this->TmpSale->find('all', array(
                'conditions' => array( 'no' => $no,
                        'employee' => $emp,
                        'store '=>$store_name,
                        'or'=>array('barcode'=>$barcode,
                            'name'=>$barcode)
    
                )
            ));
    

    谢谢

    【讨论】:

    • 嘿伙计,您不要在条件下需要 2 个数组,第 1 个就足够了。 'conditions' => array(array(..... 应该转换为'conditions' => array(....)
    • 此查询正常工作,如果您有任何查询,您只需回答,
    • 为什么需要嵌套在条件中的 2 个数组? 1个数组不够吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-25
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多