Skip to content

Commit

Permalink
adding doc blocks for new finders
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Aug 20, 2013
1 parent 5b862d5 commit 1ae0413
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
35 changes: 33 additions & 2 deletions lib/Cake/ORM/Table.php
Expand Up @@ -513,8 +513,7 @@ public function find($type, $options = []) {
}

/**
* Applies the options required to find all records on this table based
* on the passed options.
* Returns the query as passed
*
* @param \Cake\ORM\Query $query
* @param array $options
Expand All @@ -524,6 +523,27 @@ public function findAll(Query $query, array $options = []) {
return $query;
}

/**
* Sets up a query object so results appear as an indexed array, useful for any
* place where you would want a list such as for populating input select boxes.
*
* When calling this finder, the fields passed are used to determine what should
* be used as the array key, value and optionally what to group the results by.
* By default the primary key for the model is used for the key, and the display
* field.
*
* The results of this finder will be in the following form:
*
* [
* 1 => 'value for id 1',
* 2 => 'value for id 2',
* 4 => 'value for id 4'
* ]
*
* @param \Cake\ORM\Query $query
* @param array $options
* @return \Cake\ORM\Query
*/
public function findList(Query $query, array $options = []) {
$mapper = function($key, $row, $mr) use (&$columns) {
if (empty($columns)) {
Expand Down Expand Up @@ -551,6 +571,17 @@ public function findList(Query $query, array $options = []) {
return $query->mapReduce($mapper, $reducer);
}

/**
* Results for this finder will be a nested array, and is appropriate if you want
* to use the parent_id field of your model data to build nested results.
*
* Values belonging to a parent row based on their parent_id value will be
* recursively nested inside the parent row values using the `children` property
*
* @param \Cake\ORM\Query $query
* @param array $options
* @return \Cake\ORM\Query
*/
public function findThreaded(Query $query, array $options = []) {
$parents = [];
$mapper = function($key, $row, $mr) use (&$parents) {
Expand Down
5 changes: 5 additions & 0 deletions lib/Cake/Test/TestCase/ORM/TableTest.php
Expand Up @@ -539,6 +539,11 @@ public function testFindList() {
$this->assertSame($expected, $query->toArray());
}

/**
* Tests find('threaded')
*
* @return void
*/
public function testFindThreaded() {
$table = new Table(['table' => 'categories', 'connection' => $this->connection]);
$expected = [
Expand Down

0 comments on commit 1ae0413

Please sign in to comment.