开发者

Conditions with And Or statements

I got this figured out. Here is the solution:

'conditions'=>array(
     'OR' => array(
          array('EavAttribute.attribute_code'=>'lastname'),                            
          array('EavAttribute.attribute_code'=>'firstname')
      ),
     'AND' => array(
          array('UserEntityVarchar.entity_id'=>$id)
     )
)

I am trying to convert this query into a cakephp query and I am having a bit of trouble with the conditions. No matter how I format the conditions, I always end up with the second query below.

Any help with this is greatly appreciated.

This is the query I am trying to replicate:

SELECT
    `UserEntityVarchar`.`value_id`,
    `UserEntityVarchar`.`attribute_id`,
    `UserEntityVarchar`.`entity_id`,
    `UserEntityVarchar`.`value`,
    `EavAttribute`.`attribute_code`
FROM
    `user_entity_varchars` AS `UserEntityVarchar`
LEFT JOIN `eav_attributes` AS `EavAttribute` ON(
    `UserEntityVarchar`.`attribute_id` = `EavAttribute`.`attribute_id`)
WHERE
     (UserEntityVarchar.entity_id = 1 AND
     EavAttribute.attribute_code = 'firstname') OR
     (UserEntityVarchar.entity_id = 1 AND
     EavAttribute.attribute_code = 'lastname')
开发者_StackOverflow中文版

This is the query I keep getting no matter how I format my condition:

SELECT
    `UserEntityVarchar`.`value_id`,
    `UserEntityVarchar`.`attribute_id`,
    `UserEntityVarchar`.`entity_id`,
    `UserEntityVarchar`.`value`,
    `EavAttribute`.`attribute_code`
FROM
    `user_entity_varchars` AS `UserEntityVarchar`
LEFT JOIN `eav_attributes` AS `EavAttribute` ON(
    `UserEntityVarchar`.`attribute_id` = `EavAttribute`.`attribute_id`)
WHERE 
    ((`UserEntityVarchar`.`entity_id` = 1)
AND
    (`EavAttribute`.`attribute_code` = 'firstname'))
AND
    ((`UserEntityVarchar`.`entity_id` = 1)
AND
    (`EavAttribute`.`attribute_code` = 'lastname'))

This is the condition that I am using:

'conditions'=>array(
    array(
     array('UserEntityVarchar.entity_id'=>$id),
     array('AND ' => array('EavAttribute.attribute_code'=>'firstname'))

    ),
    array('OR' =>
         array('UserEntityVarchar.entity_id'=>$id),
         array('AND ' => array('EavAttribute.attribute_code'=>'firstname'))
    )
)


You should be able to do it like this:

$this->Model->find('all', array('conditions' => array('OR' => array(

              array('UserEntityVarchar.entity_id' => 1, 
              'EavAttribute.attribute_code' => 'firstname'),

              array('UserEntityVarchar.entity_id' => 1, 
              'EavAttribute.attribute_code' => 'lastname')))));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜