Select all except one field in cakephp 3 query

后端 未结 2 469
天涯浪人
天涯浪人 2021-01-21 16:42

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

Ex. $this->select(\'fname\', \'lname\', \'mname\', \'email\', \'password\', \'status\', \'crea         


        
相关标签:
2条回答
  • 2021-01-21 17:20

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

    $model->hiddenProperties(['modified', 'created']);
    
    0 讨论(0)
  • 2021-01-21 17:29

    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
    0 讨论(0)
提交回复
热议问题