![](/img/trans.png)
[英]Writing conditions in cakephp model associations hasMany and belongsTo
[英]Searching records conditions with hasMany model Cakephp3
我有具有hasMany OrderProducts模型的Orders模型。 下面是我的代碼。我使用DataTable jquery插件添加了搜索功能。每個OrderProducts都包含ref_code。
我想添加搜索,例如獲取包含OrderProducts的所有Order,其中OrderProducts包含ref_code GWG-SC-001(B)或在搜索字段中輸入的任何用戶。
OrderTable.php
namespace App\Model\Table;
use Cake\ORM\Table;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\Validation\Validator;
use Cake\ORM\TableRegistry;
use Cake\I18n\Time;
class OrdersTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public $actsAs = array('Containable');
public function initialize(array $config)
{
parent::initialize($config);
$this->table('orders');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Users', [
'propertyName' => 'Users',
'foreignKey' => 'user_id',
'className' => 'Users'
]);
$this->belongsTo('OrderStatus', [
'propertyName' => 'OrderStatus',
'foreignKey' => 'order_status_id',
'className' => 'OrderStatus'
]);
$this->hasMany('OrderProducts', [
'className' => 'OrderProducts',
'foreignKey' => 'order_id',
'className' => 'OrderProducts'
]);
$this->hasMany('OrderPayments', [
'propertyName' => 'OrderPayments',
'foreignKey' => 'order_id',
'className' => 'OrderPayments'
]);
}
}
/**
* [getOrdersList description]
* @param [type] $data [description]
* @return [type] [description]
*/
public function getOrdersList($data, $user_id = null)
{
$orderelem = $data['columns'][$data['order'][0]['column']]['name'];
$ordertype = $data['order'][0]['dir'];
$offset = $data['start'];
$limit = $data['length'];
$conditions = array("Orders.payment_status <>" => 0);
if (!empty($data['search']['value'])) {
$like = '%' . trim($data['search']['value']) . '%';
$like2 = $data['search']['value'];
$orValues = array("Order Received");
if (strpos($data['search']['value'], 'Status:') !== false) {
$data['search']['value'] = str_replace('Status: ', '', $data['search']['value']);
$orValues = explode("|", $data['search']['value']);
}
$conditions[] = array(
"Orders.payment_status <>" => 0,
'OR' => [
'Users.first_name LIKE' => $like,
'Users.last_name LIKE' => $like,
'Users.title LIKE' => $like,
'Orders.order_number LIKE' => $like2,
//'Orders.quantity_count LIKE' => $like2,
//'Orders.all_total LIKE' => $like2,
'Orders.tracking_number LIKE' => $like,
'OrderStatus.title' => $like,
'OrderProducts.ref_code' => $like, // this is not working
]
);
if (!empty($orValues)) {
$conditions[0]['OR']['OrderStatus.title IN'] = $orValues;
}
}
$data2 = $this->find('all')
->select([
'id', 'order_number', 'order_type', 'created', 'tracking_number',
'delivery_methods_id', 'order_status_id', 'quantity_count',
'all_total', 'payment_status', 'is_allow_final_payment'
])
->contain([
'Users' => function ($u) {
return $u->select(['title', 'first_name', 'last_name']);
},
'OrderStatus' => function ($os) {
return $os->select(['status' => 'OrderStatus.title']);
},
'OrderProducts'
]);
if ($user_id) {
$data2 = $data2->where(['user_id' => $user_id])->andWhere($conditions);
} else {
$data2 = $data2->where($conditions);
}
$data2 = $data2->limit($limit)
->order([$orderelem => $ordertype])
->offset($offset)
->toArray();
return $data2;
}
當我嘗試此操作時,出現以下錯誤:
“ message”:“ [PDOException] SQLSTATE [42S22]:找不到列:1054 where子句中的未知列OrderProducts.ref_code
請幫我解決這個問題。
下面的查詢正在生成:
SELECT
Orders.id AS `Orders__id`,
Orders.order_number AS `Orders__order_number`,
Orders.order_type AS `Orders__order_type`,
Orders.created AS `Orders__created`,
Orders.tracking_number AS `Orders__tracking_number`,
Orders.delivery_methods_id AS `Orders__delivery_methods_id`,
Orders.order_status_id AS `Orders__order_status_id`,
Orders.quantity_count AS `Orders__quantity_count`,
Orders.all_total AS `Orders__all_total`,
Orders.payment_status AS `Orders__payment_status`,
Orders.is_allow_final_payment AS `Orders__is_allow_final_payment`,
Users.title AS `Users__title`,
Users.first_name AS `Users__first_name`,
Users.last_name AS `Users__last_name`,
OrderStatus.title AS `status`
FROM
orders Orders
LEFT JOIN
users Users
ON Users.id =
(
Orders.user_id
)
LEFT JOIN
order_status OrderStatus
ON OrderStatus.id =
(
Orders.order_status_id
)
WHERE
(
Orders.payment_status \u003C\u003E :c0
AND
(
Orders.payment_status \u003C\u003E :c1
AND
(
Users.first_name like :c2
OR Users.last_name like :c3
OR Users.title like :c4
OR Orders.order_number like :c5
OR Orders.tracking_number like :c6
OR OrderStatus.title like :c7
OR OrderProducts.ref_code like :c8
OR OrderStatus.title in
(
:c9
)
)
)
)
ORDER BY
Orders.created desc LIMIT 25 OFFSET 25
當您使用“包含”時,它通常實際上為那些包含的模型生成單獨的查詢。 如您所見,您的“生成的查詢”實際上並不包含OrderProducts
,因此,在這種情況下,它將為OrderProducts
生成一個單獨的查詢,然后將結果組合在一起。 因此,您不能以查詢中未包含的字段為條件。
TLDR:如果要添加針對其他模型的條件,則可以包含聯接,而不是包含OrderProducts。
根據您的設置,您也許可以將joinType
指定為INNER
進行關聯。
如果沒有,您始終可以按照本書中指定的方式進行內部聯接: https : //book.cakephp.org/3.0/en/orm/query-builder.html#using-innerjoinwith
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.