Select all except one field in cakephp 3 query

≡放荡痞女 提交于 2019-12-20 04:16:48

问题


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.


回答1:


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

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



回答2:


You could just find all and then at runtime hide what you dont need

$model->hiddenProperties(['modified', 'created']);


来源:https://stackoverflow.com/questions/39719618/select-all-except-one-field-in-cakephp-3-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!