Browse Source

Merge branch 'master' into 3.next

Mark Story 7 years ago
parent
commit
8db9cffb1e
3 changed files with 32 additions and 2 deletions
  1. 29 1
      src/ORM/Query.php
  2. 2 1
      src/Utility/Inflector.php
  3. 1 0
      tests/TestCase/Utility/InflectorTest.php

+ 29 - 1
src/ORM/Query.php

@@ -170,7 +170,35 @@ class Query extends DatabaseQuery implements JsonSerializable, QueryInterface
     }
 
     /**
-     * {@inheritDoc}
+     * Adds new fields to be returned by a `SELECT` statement when this query is
+     * executed. Fields can be passed as an array of strings, array of expression
+     * objects, a single expression or a single string.
+     *
+     * If an array is passed, keys will be used to alias fields using the value as the
+     * real field to be aliased. It is possible to alias strings, Expression objects or
+     * even other Query objects.
+     *
+     * If a callable function is passed, the returning array of the function will
+     * be used as the list of fields.
+     *
+     * By default this function will append any passed argument to the list of fields
+     * to be selected, unless the second argument is set to true.
+     *
+     * ### Examples:
+     *
+     * ```
+     * $query->select(['id', 'title']); // Produces SELECT id, title
+     * $query->select(['author' => 'author_id']); // Appends author: SELECT id, title, author_id as author
+     * $query->select('id', true); // Resets the list: SELECT id
+     * $query->select(['total' => $countQuery]); // SELECT id, (SELECT ...) AS total
+     * $query->select(function ($query) {
+     *     return ['article_id', 'total' => $query->count('*')];
+     * })
+     * ```
+     *
+     * By default no fields are selected, if you have an instance of `Cake\ORM\Query` and try to append
+     * fields you should also call `Cake\ORM\Query::enableAutoFields()` to select the default fields
+     * from the table.
      *
      * If you pass an instance of a `Cake\ORM\Table` or `Cake\ORM\Association` class,
      * all the fields in the schema of the table or the association will be added to

+ 2 - 1
src/Utility/Inflector.php

@@ -144,7 +144,8 @@ class Inflector
         'goose' => 'geese',
         'foot' => 'feet',
         'foe' => 'foes',
-        'sieve' => 'sieves'
+        'sieve' => 'sieves',
+        'cache' => 'caches',
     ];
 
     /**

+ 1 - 0
tests/TestCase/Utility/InflectorTest.php

@@ -189,6 +189,7 @@ class InflectorTest extends TestCase
             ['blue_octopus', 'blue_octopuses'],
             ['chef', 'chefs'],
             ['', ''],
+            ['cache', 'caches'],
         ];
     }