# |
Jan 23rd 2020, 20:37 |
ndm |
On a serious note |
# |
Jan 23rd 2020, 20:37 |
ndm |
You need to be more open for new experiences :P |
# |
Jan 23rd 2020, 20:36 |
k4t |
@ndm do you know what my problem is? |
# |
Jan 23rd 2020, 20:35 |
k4t |
group by on DB is not needed |
# |
Jan 23rd 2020, 20:34 |
challgren |
Im not sure though try both |
# |
Jan 23rd 2020, 20:33 |
challgren |
database, |
# |
Jan 23rd 2020, 20:33 |
k4t |
groupBy on database or returned ResultsSet? |
# |
Jan 23rd 2020, 20:32 |
challgren |
groupBy()? |
# |
Jan 23rd 2020, 20:31 |
k4t |
$results will contain a lot of duplicated items (Posts), and each Post in that array will contain Comment under _matchingData |
# |
Jan 23rd 2020, 20:30 |
k4t |
which is a big pain for me |
# |
Jan 23rd 2020, 20:30 |
k4t |
so ->matching is fine, it will give me SQL query which I want but result of association collection is not nested in $results array |
# |
Jan 23rd 2020, 20:28 |
challgren |
Sorry jumping ahead |
# |
Jan 23rd 2020, 20:28 |
challgren |
Ahh ok |
# |
Jan 23rd 2020, 20:28 |
k4t |
and these additional queries added by ->contain are completely unnecessary as I already got all required data from db thanks to ->matching |
# |
Jan 23rd 2020, 20:28 |
challgren |
You would need to do ->contain([‘Comments’ => function($q) { return $q->where([‘Comments.user_id’ => 2]) }]) |
# |
Jan 23rd 2020, 20:27 |
challgren |
It contains all the comments |
# |
Jan 23rd 2020, 20:27 |
k4t |
but my problem is that results from ->matching are formated different then if I use ->contain |
# |
Jan 23rd 2020, 20:26 |
challgren |
True |
# |
Jan 23rd 2020, 20:26 |
k4t |
if I add contain it will again generate additional queries |
# |
Jan 23rd 2020, 20:26 |
challgren |
Are you missing a contain()? |
# |
Jan 23rd 2020, 20:25 |
k4t |
let me explain |
# |
Jan 23rd 2020, 20:25 |
k4t |
It will, and it will generally generate query which interests me but my problem is that $results now contain data in "bad" format |
# |
Jan 23rd 2020, 20:25 |
challgren |
Ahh theres the ORM expert |
# |
Jan 23rd 2020, 20:25 |
ndm |
it will |
# |
Jan 23rd 2020, 20:24 |
challgren |
Im not sure on that one |
# |
Jan 23rd 2020, 20:23 |
k4t |
let's say we have relation Posts hasMany Comments and I will do: $results = $posts->find()->matching('Comments', function($q) { return $q->where(['Comments.user_id' => 2]) })->toArray(); It will generate 1 SQL query with INNER JOIN, right? |
# |
Jan 23rd 2020, 20:20 |
challgren |
Im not an ORM expert by any means but Ill try to help my best |
# |
Jan 23rd 2020, 20:17 |
k4t |
you are right but hmm... generally I have a big problem with CakePHP ORM and I can not solve it from years... let me write some details (sec) |
# |
Jan 23rd 2020, 20:15 |
challgren |
For example your hasMany is Comments, and you have a Post, with what your talking about you would have 30 duplicate Posts for the 30 comments |
# |
Jan 23rd 2020, 20:14 |
challgren |
And not duplicating the original data |
# |
Jan 23rd 2020, 20:14 |
challgren |
How would you do it in regular SQL without 2 queries? |
# |
Jan 23rd 2020, 20:14 |
k4t |
it will still generate two queries |
# |
Jan 23rd 2020, 20:14 |
challgren |
But you can change the strategy https://book.cakephp.org/3/en/orm/associations.html#hasmany-associations |
# |
Jan 23rd 2020, 20:13 |
challgren |
How else would you do it without duplicating the original table entries in the results? |
# |
Jan 23rd 2020, 20:05 |
k4t |
is it possible to change it? |
# |
Jan 23rd 2020, 20:05 |
k4t |
Why CakePHP 3 for hasMany relations generates 2 selects instead of one with JOIN? |
# |
Jan 23rd 2020, 19:19 |
challgren |
You could also do a Plugin approach too. It just really depends on what your trying to accomplish specifically |
# |
Jan 23rd 2020, 19:18 |
mocelle |
Hm... Good idea. I will try it |
# |
Jan 23rd 2020, 19:18 |
challgren |
You could also use Traits |
# |
Jan 23rd 2020, 19:17 |
challgren |
Like validators? |
# |
Jan 23rd 2020, 19:17 |
mocelle |
Trying to separate BL to own directory |