在蛋糕php写查询

时间:2015-01-08 15:23:11

标签: mysql cakephp

如何在cakephp 2.0语法中编写以下查询

SELECT `customers`.*
FROM `group_customers`, `customers`
WHERE 
`group_customers`.`customer_id`=`customers`.`id`
AND `group_customers`.`group_id`=7
AND
(`customers`.`name` LIKE 'O%' OR `customers`.`company` LIKE 'O%')

其中customers和group_customers是两个表并具有Customer和Groupcustomer模型

plz帮助我获得正确的cakephp语法,因为我尝试使用以下方式

this->Customer->find('all',array(
                'joins' => array(
                array(
                'table' => 'group_customers',
                'alias' => 'GroupCustomer',
                'type' => 'INNER',
                'conditions' => array(
                'GroupCustomer.customer_id'=>'Customer.id',
                'GroupCustomer.group_id'=>$grpid ))),
                'conditions' => array('OR'=>array('Customer.name LIKE'
                                 =>$text.'%','Customer.company LIKE'=>$text.'%'))
                    ));

1 个答案:

答案 0 :(得分:0)

您可以使用此代码。

加入group_customers与客户

$this->GroupCustomer->bindModel(
    array(
        'belongsTo' => array(
            'Customer' => array(
                'className' => 'Customer',
                'foreignKey' => 'customer_id'
            )
        )
    )
);

获取GroupCustomer和客户记录

$this->GroupCustomer->find(
    'all',
    array(
        'GroupCustomer.group_id' => 7,
        'OR' => array(
            'Customer.name LIKE' => '0%'
            'Customer.company LIKE' => '0%'
        )
    )
);

谢谢..!