0

你好

我需要将以下 sql 查询转换为 cakephp find() 格式。它自己的查询工作正常,但我需要更改它。

$this->Part->query(
    "SELECT `parts`.`id`,`parts`.`part_name` 
     FROM `parts`  
     LEFT JOIN ( 
         SELECT `op` . * 
         FROM `order_parts` AS `op` 
         WHERE `op`.`order_id` =".$this->Session->read('orderid')."
     ) AS `vT` 
     ON ( `parts`.`id` = `vT`.`part_id` )
     WHERE `vT`.`part_id` IS NULL"
);  

谢谢

4

1 回答 1

1

如果您的关系是 Order HABTM Part 并且您有一个包含列的表 orders_parts:id、order_id、part_id,您应该能够执行以下操作:

首先,获取按顺序排列的部件的 ID:

//each Part has one OrdersPart per order
$this->Part->bindModel(array('hasOne' => array('OrdersParts')));
$parts = $this->Part->find('list', array(
  'fields' => array('Part.name'),
  'conditions' => array(
    'OrdersParts.order_id' => $this->Session->read('orderid'),
  ),
  'recursive' => 2
));

现在获取不按顺序排列的部分:

$this->Part->find('all', array(
  'conditions' => array(
    "NOT" => array('Part.id' => array_keys($parts))
  ),
));
于 2012-10-12T10:46:34.570 回答