Cakephp3查询模型
Cakephp3 query model
这是我的产品型号:
$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
的产品
Cao Thế Cường,你试过这样的关系查询吗:
$result = Products->find()->contain(['RefundProducts' => function ($q) {
return $q
->select(['field1', 'field2'])
->where(['refunded_product' => true])]);
使用where
检查是否使用了相关table的一列(本例中为RefundProducts
):
// in your query-object
$query->where(['Table.column IS NOT NULL']);
这是 matching()
方法的工作,它将创建与 RefundProducts
的 INNER JOIN,因此您将得到 only Products
有一些 RefundProducts
。 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
的 Products
,但仅当满足 $refund_condition
时才会包含 RefundProducts
(因此 RefundProducts
可以返回为空)。或者,根据你要过滤的内容,你可以这样做
->contain(['RefundProducts'])
->matching('RefundProducts', function ($q) use ($refund_condition) {
return $q->where($refund_condition);
})
这是我的产品型号:
$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
的产品Cao Thế Cường,你试过这样的关系查询吗:
$result = Products->find()->contain(['RefundProducts' => function ($q) {
return $q
->select(['field1', 'field2'])
->where(['refunded_product' => true])]);
使用where
检查是否使用了相关table的一列(本例中为RefundProducts
):
// in your query-object
$query->where(['Table.column IS NOT NULL']);
这是 matching()
方法的工作,它将创建与 RefundProducts
的 INNER JOIN,因此您将得到 only Products
有一些 RefundProducts
。 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
的 Products
,但仅当满足 $refund_condition
时才会包含 RefundProducts
(因此 RefundProducts
可以返回为空)。或者,根据你要过滤的内容,你可以这样做
->contain(['RefundProducts'])
->matching('RefundProducts', function ($q) use ($refund_condition) {
return $q->where($refund_condition);
})