问题
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