选择cakephp 3查询中除一个字段以外的所有字段

编程入门 行业动态 更新时间:2024-10-27 14:21:23
本文介绍了选择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']); );

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

另请参见

  • 菜谱>数据库访问& ORM>查询生成器>选择特定字段

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: github/cakephp/cakephp/issues/6904

See also

  • Cookbook > Database Access & ORM > Query Builder > Selecting Specific Fields

更多推荐

选择cakephp 3查询中除一个字段以外的所有字段

本文发布于:2023-10-28 13:52:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1536811.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   cakephp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!