选择cakephp 3查询中除一个字段以外的所有字段 [英] Select all except one field in cakephp 3 query

查看:111
本文介绍了选择cakephp 3查询中除一个字段以外的所有字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想选择cakephp 3中除一个字段之外的所有字段.

Ex. $this->select('fname', 'lname', 'mname', 'email', 'password', 'status', 'created', 'modified');

在这里我想选择除创建和修改之外的所有字段,因为我的另一个表具有约30个字段,并且我想选择28个字段,并且不想在select函数中提及每个字段,因为这很耗时. /p>

能否请您提出一种更好的方法.

解决方案

从CakePHP 3.6开始,引入了selectAllExcept()方法,该方法将从属于给定表的模式中选择所有列,但传入的列除外第二个参数:

$query->selectAllExcept($table, ['modified', 'created']);

在早期版本中,您必须手动执行此操作,即从架构中获取所有可能的列,然后删除您不想包含的列,例如:

$query->select(
    array_diff($table->schema()->columns(), ['modified', 'created']);
);

在相关说明中,检查要求取消选择功能的以下问题: https://github.com/cakephp/cakephp/issues/6904

另请参见

I just want to select all fields except one field in cakephp 3.

Ex. $this->select('fname', 'lname', 'mname', 'email', 'password', 'status', 'created', 'modified');

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.

解决方案

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']);
);

On a related note, check the following issue that asks for a deselect functionality: https://github.com/cakephp/cakephp/issues/6904

See also

这篇关于选择cakephp 3查询中除一个字段以外的所有字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆