Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions en/orm/retrieving-data-and-resultsets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ methods will let you re-use your queries and make testing easier.
By default queries and result sets will return :doc:`/orm/entities` objects. You
can retrieve basic arrays by disabling hydration::

$query->enableHydration(false);
// Prior to 3.4.0
$query->hydrate(false);

// $data is ResultSet that contains array data.
Expand Down Expand Up @@ -228,14 +230,16 @@ data::

With no additional options the keys of ``$data`` will be the primary key of your
table, while the values will be the 'displayField' of the table. You can use the
``displayField()`` method on a table object to configure the display field of
``setDisplayField()`` method on a table object to configure the display field of
a table::

class ArticlesTable extends Table
{

public function initialize(array $config)
{
$this->setDisplayField('title');
// Prior to 3.4.0
$this->displayField('title');
}
}
Expand Down Expand Up @@ -312,9 +316,9 @@ the Author entity. ::
You can also fetch the label in the list directly using. ::

// In AuthorsTable::initialize():
$this->displayField('label'); // Will utilize Author::_getLabel()
$this->setDisplayField('label'); // Will utilize Author::_getLabel()
// In your finders/controller:
$query = $authors->find('list'); // Will utilize AuthorsTable::displayField()
$query = $authors->find('list'); // Will utilize AuthorsTable::getDisplayField()

Finding Threaded Data
=====================
Expand Down Expand Up @@ -630,13 +634,13 @@ to ``select()``::
->select($articles->Users)
->contain(['Users']);

Alternatively, if you have multiple associations, you can use ``autoFields()``::
Alternatively, if you have multiple associations, you can use ``enableAutoFields()``::

// Select id & title from articles, but all fields off of Users, Comments
// and Tags.
$query->select(['id', 'title'])
->contain(['Comments', 'Tags'])
->autoFields(true)
->enableAutoFields(true) // Prior to 3.4.0 use autoFields(true)
->contain(['Users' => function($q) {
return $q->autoFields(true);
}]);
Expand Down Expand Up @@ -820,7 +824,7 @@ data, you can use the ``leftJoinWith()`` function::
$query->select(['total_comments' => $query->func()->count('Comments.id')])
->leftJoinWith('Comments')
->group(['Articles.id'])
->autoFields(true);
->enableAutoFields(true); // Prior to 3.4.0 use autoFields(true);

The results for the above query will contain the article data and the
``total_comments`` property for each of them.
Expand All @@ -836,7 +840,7 @@ word, per author::
return $q->where(['Tags.name' => 'awesome']);
})
->group(['Authors.id'])
->autoFields(true);
->enableAutoFields(true); // Prior to 3.4.0 use autoFields(true);

This function will not load any columns from the specified associations into the
result set.
Expand Down Expand Up @@ -879,6 +883,8 @@ column::
Dynamically changing the strategy in this way will only apply to a specific
query. If you want to make the strategy change permanent you can do::

$articles->FirstComment->setStrategy('seelct');
// Prior to 3.4.0
$articles->FirstComment->strategy('select');

Using the ``select`` strategy is also a great way of making associations with
Expand Down Expand Up @@ -910,6 +916,8 @@ databases that limit the amount of bound parameters per query, such as

You can also make the strategy permanent for the association by doing::

$articles->Comments->setStrategy('subquery');
// Prior to 3.4.0
$articles->Comments->strategy('subquery');

Lazy Loading Associations
Expand All @@ -935,6 +943,8 @@ set multiple times, or cache and iterate the results. If you need work with
a data set that does not fit into memory you can disable buffering on the query
to stream results::

$query->enableBufferResults(false);
// Prior to 3.4.0
$query->bufferResults(false);

Turning buffering off has a few caveats:
Expand Down Expand Up @@ -1153,7 +1163,7 @@ Finally, we put everything together::
$wordCount = $articles->find()
->where(['published' => true])
->andWhere(['published_date >=' => new DateTime('2014-01-01')])
->hydrate(false)
->enableHydrate(false) // Prior to 3.4.0 use hydrate(false)
->mapReduce($mapper, $reducer)
->toArray();

Expand Down Expand Up @@ -1190,21 +1200,21 @@ The intermediate array will be like the following::
...
]

Positive numbers mean that a user, indicated with the first-level key, is
Positive numbers mean that a user, indicated with the first-level key, is
following them, and negative numbers mean that the user is followed by them.

Now it's time to reduce it. For each call to the reducer, it will receive a list
of followers per user::

$reducer = function ($friends, $user, $mr) {
$fakeFriends = [];

foreach ($friends as $friend) {
if ($friend > 0 && !in_array(-$friend, $friends)) {
$fakeFriends[] = $friend;
}
}

if ($fakeFriends) {
$mr->emit($fakeFriends, $user);
}
Expand All @@ -1213,7 +1223,7 @@ of followers per user::
And we supply our functions to a query::

$fakeFriends = $friends->find()
->hydrate(false)
->enableHydrate(false) // Prior to 3.4.0 use hydrate(false)
->mapReduce($mapper, $reducer)
->toArray();

Expand Down Expand Up @@ -1292,4 +1302,3 @@ calling the method with both parameters as null and the third parameter
(overwrite) as ``true``::

$query->mapReduce(null, null, true);