Hi everyone. Is it possible to use select inside contain when using get method like:
$user = $this->Users->get($id, [
‘contain’ => // some fields in Articles table
]);
Hi everyone. Is it possible to use select inside contain when using get method like:
$user = $this->Users->get($id, [
‘contain’ => // some fields in Articles table
]);
Thanks the pagination example works for me.
Then contain
array inside the ->get($id)
behaves the same as if you would do it in a ->find()->contain()
call
This great topic I always found it weird why the find()->contain() but the get( ,[‘contain’]) why not just have get()->contain() ?
Well ->get()
directly returns an entity, not a query instance like ->find()
does.
So ->get()
directly executes SQL and the base concept of CakePHP is to not lazy load data and instead always eager load (to prevent N+1 queries)
Therefore (theoretically) having something like
$entity = $this->MyModel->get($id)->contain(['SomeAssoc']);
Would execute multiple queries, which would not be best practice.
Thats why
$entity = $this->MyModel->get($id, ['contain' => ['SomeAssoc']]);
is the correct way.