如何通过在 CakePHP 3 中组合/拥有两个字段来创建 keyValue 对(显示字段)? [英] How do I create a keyValue pair (display field) by combining/having two fields in CakePHP 3?
问题描述
我需要帮助使用 find 或 get<在键值对中获取两个值(对于人类友好的下拉菜单)/strong> 或其他任何东西.我需要有关在 CakePHP 中执行此操作的最简单方法的帮助.
I need help getting two values (for human friendly dropdown) in a key-value pair using find or get or anything else. I need help with the simplest method of doing this in CakePHP.
这是我的尝试:
在我的控制器中
in my controller
$users = $this->LocationsUser->Users->find('list', [
'limit' => 1,
'keyField' => 'id',
'valueField' => ['first_name', 'last_name']
])->where(['id' => $id]);
在我看来
In my view
echo $this->Form->input('user_id', [
'options' => $users,
'type' => 'select',
'multiple' => false,
]);
我的下拉列表中的结果:
The result on my drop-down:
<option value="10">Fabian;Pankiers</option>
请参阅我需要不带分号 ";" 的结果.现在我可以使用 javascript 来删除分号,但那是矫枉过正.在 CakePHP 3 中是否有一种简单的方法来实现这一点?
See I need a result without the semicolon ";". Now I can use javascript to remove the semicolon but then thats overkill. Is there a simple way to achieve this within CakePHP 3?
推荐答案
有多种方法可以解决这个问题,这里是其中的三种:
There are various ways to solve this, here's three of them:
使用回调并使用结果中的值将值拼接在一起.
Use a callback and stitch the value together by using values from the results.
$query = $this->LocationsUser->Users
->find('list', [
'valueField' => function ($row) {
return $row['first_name'] . ' ' . $row['last_name'];
}
])
->where(['id' => $id]);
另见
使用结果格式化程序将值拼接在一起,这也是 list
查找器在内部所做的,与上面的示例基本相同,只是更复杂.
Use a result formatter that stitches the values together, that's what the list
finder does internally too, and basically the same as the above example, just more complicated.
$query = $this->LocationsUser->Users
->find()
->select(['id', 'first_name', 'last_name'])
->formatResults(function($results) {
/* @var $results CakeDatasourceResultSetInterface|CakeCollectionCollectionInterface */
return $results->combine(
'id',
function($row) {
return $row['first_name'] . ' ' . $row['last_name'];
}
);
})
->where(['id' => $id]);
结合虚拟属性
您也可以在此处使用(重新)使用虚拟属性,例如在您的 User
实体类中定义的属性名称 full_name
:
Combined with virtual properties
You could also use (re-)use virtual properties here, given for example a property name full_name
defined in your User
entity class:
protected function _getFullName()
{
return $this->_properties['first_name'] . ' ' . $this->_properties['last_name'];
}
您可以只在格式化程序中返回它,而不是再次手动组合这两个字段:
You could just return that in the formatter instead of manually combining the two fields again:
function($row) {
return $row['full_name'];
}
在 valueField
示例中也可以这样做.
The same could be done in the valueField
example.
另见
- Cookbook > 数据库访问&ORM > 查询构建器 > 查询是集合对象
- Cookbook > 数据库访问 &ORM > 查询生成器 > 添加计算字段
- Cookbook > 数据库访问 &ORM > 实体 > 创建虚拟属性
添加一个计算域并将其用于 valueField
选项
Add a computed field and use that one for the valueField
option
$query = $this->LocationsUser->Users
->find('list', [
'keyField' => 'id',
'valueField' => 'concatenated'
]);
$query
->select([
'id',
'concatenated' => $query->func()->concat([
'first_name' => 'literal',
' ',
'last_name' => 'literal'
])
])
->where(['id' => $id]);
另见
如果您希望始终应用您的自定义列表,您可以覆盖 Users
表类中的 list
查找器.
If you want your custom list to be always applied, you could override the list
finder in your Users
table class.
use CakeORMQuery;
// ...
public function findList(Query $query, array $options)
{
return $query
->find()
->select(['id', 'first_name', 'last_name'])
// etc ...
;
}
这样你就可以继续使用find('list')
.
That way you can continue to use find('list')
.
另见
这篇关于如何通过在 CakePHP 3 中组合/拥有两个字段来创建 keyValue 对(显示字段)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!