Extract on resultset

Have some problem where I do

 $this->paginate = [
            'contain' => ['Categories'],

$articles = $this->Articles->find('byState',['state_id' => '3']);

$cat_array = $articles->all()->extract('category_id')->toArray();
$categories = $this->Articles->Categories->find('list')->where(['id IN' => $cat_array])->order(['Categories.name']);

$articles = $this->paginate($articles);

After I do this, sure I have the categories data in categories var. But the contained relations on $articles seem to be gone now, after performing the extract operation. Before it did not work like that in cakephp 3. I assume something has changed why this is not working anymore as intended?

For some reason the this->paginate options do not work after having used extract on the resultset. Where normally the this paginate will use those options…

You already executed the query with


how should it then paginate with a SQL limit?

to use extract you need to do all() first otherwise you have deprecated warning, but the limit does work on the paginate options, it is just the contain that doesnt work…

I am pretty confused by some of the query things.

If I do

$all_articles = $articles->find();

$some_articles = $all_articles->where([something])

then the results in all_articles are also changed?

How can you reuse the same result/query to perform different tasks on it?

The query object mutates itself and returns itself, otherwise something like this wouldn’t be possible

$query = $this->Articles->find('byState',['state_id' => '3'])
    ->where(['myfield' => 'something'])

I’d rather recommend you look into custom finders to create reusabled query snippets

otherwise you can of course manually create a clone of the object as well

$copiedQuery = clone $articles;

ok Thanks. Had the problem where first select the articles, then after use the same result to extract categories with count. But then the original data was gone…