如何使用FriendsOfCake搜索插件来筛选相关数据CakePHP 3.1.0 [英] How to Filter Associated Data using FriendsOfCake Search plugin for CakePHP 3.1.0
问题描述
插件: FriendsOfCake\Search
CakePHP: 3.1.0
我目前正在添加对 index()
方法。我需要能够搜索订单我的名称的用户谁下订单。每个订单都与 Users
模型相关联:
I'm currently adding the ability to filter my Orders controller on the index()
method. I'm needing to be able to search Orders my the name of the User who placed the order. Each order is associated with the Users
model:
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
当我建立 searchConfiguration()
在 OrdersTable.php
文件中我有以下:
When I build my searchConfiguration()
method in the OrdersTable.php
file I have the following:
->value('first_name', [
'field' => $this->aliasField('Users.first_name')
])
->value('last_name', [
'field' => $this->aliasField('Users.last_name')
])
订单索引()方法
$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
我得到错误:
This loads fine when I'm not passing any parameters to the query, however, as soon as I try and search by first_name
or last_name
I get the error:
错误:SQLSTATE [42S22]:未找到列:1054'where clause'中的未知列'Orders.Users.first_name'
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Orders.Users.first_name' in 'where clause'
根据错误,它是追加订单。到我想要搜索的字段。从我可以告诉 CakePHP
有两个方法 aliasField()
一个在 \var\\ \\www\< project_name> \vendors\cakephp\cakephp\src\ORM\Query.php
Based on the error, it's appending Orders. to the field that I'm trying to search by. From what I can tell CakePHP
has 2 methods aliasField()
one in \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没有,所以我假设这里使用的方法。
It appears that Query.php
will allow you to specify the $alias
for the field, however, Table.php does not, so I assume that's the method that's being used here.
任何人都可以指导我如何过滤相关表格中的数据。
Can anyone give me guidance on how to filter on data contained in an associated table?
推荐答案
当您已经提供别名时,不要使用 aliasField()
Simply either do not use aliasField()
when you're already supplying the alias
'field' => 'Users.first_name'
或使用 aliasField()
or use aliasField()
on the associated Users
table
'field' => $this->Users->target()->aliasField('first_name')
使用 Query :: aliasField()
将是完全错误的,因为这将返回一个 key => value
数组,用于 Query :: select()
。
Using Query::aliasField()
would be totally wrong, as this will return a key => value
array for use with Query::select()
.
这篇关于如何使用FriendsOfCake搜索插件来筛选相关数据CakePHP 3.1.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!