0

我有以下三个模型Product,,,OrderOrderDetail

Product有以下关联:

public $hasMany = array('OrderDetail');

Order有以下关联:

public $hasMany = array('OrderDetail');

OrderDetail具有以下关联:

public $belongsTo = array('Order');
public $belongsTo = array('Product');

我正在使用这种Containable行为并想要find()产品。

如何Order在 find() 调用的条件部分使用模型中的字段?

在我的Product模型中是这样的:

$this->find('all', array('
    conditions' => array(
        'Order.order_date' => '2011-09-12'
    )
));
4

3 回答 3

0

使用“包含”索引来指定订单模型和相关条件。确保您在Product模型上使用可包含行为。

$this->find('all', array(
  'contain' => array(
    'Order' => array(
      'conditions' => array(
        'Order.order_date' => '2011-09-12'
      )
    )
  ),
));

此外,您可能想看看您的模型关系。我认为您可能在问题中指定错误。

于 2011-11-10T16:40:10.617 回答
0

首先将 $belongsTo 数组合并到

public $belongsTo = array('Order', 'Product');

然后再试一次

于 2011-11-11T06:06:49.087 回答
0

你无法以这种方式实现你想要的。如果您查看 Cake dos 的查询,您就会明白为什么。Containable 执行多个查询,然后将它们组合起来。

实现您想要的最简单的方法是从 Order 模型的角度执行查询,因为它是您要过滤的模型。如果这不是一个选项,请参阅:

在这里查看一些方法

于 2011-11-11T07:56:56.830 回答