选择 cakephp 3 查询中除一个字段之外的所有字段 [英] Select all except one field in cakephp 3 query
问题描述
我只想选择 cakephp 3 中除了一个字段之外的所有字段.
I just want to select all fields except one field in cakephp 3.
Ex. $this->select('fname', 'lname', 'mname', 'email', 'password', 'status', 'created', 'modified');
这里我想选择除创建和修改之外的所有字段,因为我的另一个表有大约 30 个字段,我想选择 28 个字段,不想在 select 函数中提及每个字段,因为它很耗时.
Here i want to select all fields except created and modified because my other table have apprx 30 fields and i want to select 28 fields and don't want to mentioned each and every field in select function because it is time consuming.
你能提出更好的方法吗.
Can you please suggest a better way.
推荐答案
从 CakePHP 3.6 开始,引入了 selectAllExcept()
方法,该方法将从架构中选择属于给定的所有列表,除了在第二个参数中传递的那些列:
As of CakePHP 3.6 the selectAllExcept()
method has been introduced, which will select all columns from the schema that belongs to the given table, except those columns passed in the second argument:
$query->selectAllExcept($table, ['modified', 'created']);
在早期版本中,您必须手动执行此操作,即从架构中获取所有可能的列并删除那些您不想包含的列,例如:
In earlier versions you'd have to do that manually, ie grab all the possible columns from the schema and remove those that you don't want to include, like:
$query->select(
array_diff($table->schema()->columns(), ['modified', 'created']);
);
在相关说明中,检查以下要求取消选择功能的问题:https://github.com/cakephp/cakephp/issues/6904
On a related note, check the following issue that asks for a deselect functionality: https://github.com/cakephp/cakephp/issues/6904
另见
这篇关于选择 cakephp 3 查询中除一个字段之外的所有字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!