这是我的产品模型:
$this->table('products');
$this->belongsTo('OrderProducts', [
'foreignKey' => 'order_product_id',
'propertyName' => 'order_product',
'joinType' => 'INNER'
]);
$this->hasMany('RefundProducts', [
'foreignKey' => 'product_id',
'sort' => ['RefundProducts.created' => 'DESC'],
'propertyName' => 'refund_products',
'className' => 'RefundProducts'
]);我的问题是:
$result = $this->Products->find('all', [
'contain' => [
'RefundProducts' => [
'PriceUnits',
'conditions' => $refund_condition,
]
]
]);但是它得到了所有的产品,我只想得到有RefundProducts的产品
发布于 2015-05-28 06:25:31
这是matching()方法的任务,它将创建与RefundProducts的内部连接,因此您将只得到一些RefundProducts的Products。contain限制中的条件仅获取关联
$result = $this->Products->find()
->contain(['RefundProducts' => function ($q) use ($refund_condition) {
return $q->where($refund_condition);
}])
->matching('RefundProducts')
->distinct(['Products.id']);我不知道$refund_condition该怎么做。这个示例将获得具有某些RefundProducts的RefundProducts,但只有当$refund_condition满足时才会包含RefundProducts (因此RefundProducts可以返回为空)。或者,根据您想要过滤的内容,可以这样做。
->contain(['RefundProducts'])
->matching('RefundProducts', function ($q) use ($refund_condition) {
return $q->where($refund_condition);
})发布于 2015-05-27 11:11:59
Cao ThếCường,您是否尝试过这样的关系查询:
$result = Products->find()->contain(['RefundProducts' => function ($q) {
return $q
->select(['field1', 'field2'])
->where(['refunded_product' => true])]);发布于 2015-05-27 11:12:34
使用where检查是否使用了相关表的列(在本例中为RefundProducts):
// in your query-object
$query->where(['Table.column IS NOT NULL']);https://stackoverflow.com/questions/30479723
复制相似问题