【发布时间】:2021-03-18 22:32:26
【问题描述】:
插件:FriendsOfCake\Search
CakePHP: 3.1.0
我目前正在添加在index() 方法上过滤我的订单控制器的功能。我需要能够在下订单的用户的姓名中搜索订单。每个订单都与Users 模型相关联:
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
当我在OrdersTable.php 文件中构建我的searchConfiguration() 方法时,我有以下内容:
->value('first_name', [
'field' => $this->aliasField('Users.first_name')
])
->value('last_name', [
'field' => $this->aliasField('Users.last_name')
])
Orders index() 方法
$query = $this->Orders->find('search',
$this->Orders->filterParams($this->request->query))->contain(['Users', 'PaymentMethods', 'Industries']
)->order(['Orders.created' => 'DESC']);
$this->set('orders', $this->paginate($query));
当我没有向查询传递任何参数时,这加载正常,但是,一旦我尝试通过first_name 或last_name 进行搜索,我就会收到错误:
错误:SQLSTATE[42S22]:找不到列:1054 'where 子句'中的未知列'Orders.Users.first_name'
根据错误,它正在附加订单。到我要搜索的字段。据我所知CakePHP 有两种方法aliasField() 一种在\var\www\<project_name>\vendors\cakephp\cakephp\src\ORM\Query.php 中
public function aliasField($field, $alias = null)
{
$namespaced = strpos($field, '.') !== false;
$aliasedField = $field;
if ($namespaced) {
list($alias, $field) = explode('.', $field);
}
if (!$alias) {
$alias = $this->repository()->alias();
}
$key = sprintf('%s__%s', $alias, $field);
if (!$namespaced) {
$aliasedField = $alias . '.' . $field;
}
return [$key => $aliasedField];
}
还有一个\var\www\<project_name>\vendors\cakephp\cakephp\src\ORM\Table.php
public function aliasField($field)
{
return $this->alias() . '.' . $field;
}
看来Query.php 将允许您为该字段指定$alias,但是,Table.php 不允许,所以我认为这就是这里使用的方法。
谁能指导我如何过滤关联表中包含的数据?
【问题讨论】:
标签: php cakephp cakephp-3.1