New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
3.0 - Empty property when matching() and contain() on the same alias #5463
Comments
I hoped that people would understand that adding a matching on a joinable association would obviously put the data in matching. Apparently this is not the case... |
The case is that using I tried it on |
But I understand that using only matching would put data only into |
That's fine. I understand that it is not obvious so I will take care of it. As for the 'subquery' strategy I truly surprised that it makes a difference at all, I'm going to investigate that as well. |
What is not obvious? I am not sure if we understand each other. I say the problem is when using this on $article = $articles->matching('Categories', function(){ ... })->contain('Categories')->first();
$article->category; // This is EMPTY although it is contained
$article->_matchingData['Categories']; // This is OK |
Something that was obvious to me, but I can totally see now that it is not obvious for everyone not involved with the internals of how it works. |
I also experienced this problem: // Users belong to Cities, Cities belong to Countries
$user = $users->matching('Cities', function($query) {
return $query->where(['Cities.country_id' => 1]);
})
->contain(['Cities' => ['Countries']])
->first();
$user->city // This is OK after my little fix described below
$user->city->country; // This is EMPTY Little fix: I removed the To make it clear the SQL should look something like (without all the ORM stuff): SELECT * FROM users
INNER JOIN cities ON cities.id = users.city_id AND cities.country_id = 1
INNER JOIN countries ON countries.id = cities.country_id So it's basically filtering all users that belong to country |
Thanks for the issue report, test case and candidate fix, it was very helpful |
👍 You are welcome. Thank's for the fix. Looking forward for merge :) |
@Laykouis this working for you? I'm using 3.4.7 and experiencing the same problem! the contain property is empty (actually NULL) from the docs: "_The data from the association that is ‘matched’ will be available on the _matchingData property of entities. If you both match and contain the same association, you can expect to get both the matchingData and standard association properties in your results." |
@nilsonpena this is old, but does using both matching and contain in the same query work? Not working for me. |
@hagen00 It should work, there are tests showing that this is possible |
Ok, I got a brand new Cake install, created some simple tables and tested the interaction between contain and matching. Seems to work as expected! |
Contain doesn't work properly when using
'strategy' => 'subquery'
defined inTable
. It returns selected rows and also_matchingData
properly but the contained property is empty.This is the example code (I tried it on different one but it should do the same):
And it also doesn't work on
belongsTo
associations - the same issue,_matchingData
is there butassociated property
is empty:cakephp/src/ORM/ResultSet.php
Line 361 in dab84c6
I think this unset is doing a problem for BelongsTo associations because the associated belongsTo object is then not attached to the master entity. When I removed it, it worked.
The text was updated successfully, but these errors were encountered: