# |
Jun 27th 2018, 09:25 |
itamer |
got it :slightly_smiling_face: |
# |
Jun 27th 2018, 09:25 |
itamer |
->Mtypes->name; |
# |
Jun 27th 2018, 09:24 |
itamer |
What's the correct notation when it's a variable on a joined table? |
# |
Jun 27th 2018, 09:24 |
itamer |
My ->name; command at the end will need to change, right? |
# |
Jun 27th 2018, 09:20 |
itamer |
ok, I'm starting to work on that so I'll continue down that path. This part of the site requires very little work so I'm diving into v3 infrequently. I spend most of my time working with v2 where it's super-easy |
# |
Jun 27th 2018, 09:20 |
neon1024 |
Which sidesteps the hasMany issue by inverting the association to a belongsTo making the query join easier :slightly_smiling_face: |
# |
Jun 27th 2018, 09:19 |
neon1024 |
Ah ok, then I would just query `Finances` and contain `Mtypes` |
# |
Jun 27th 2018, 09:19 |
itamer |
ok, thanks, I'll try that - I only need the Mtypes name field value for the most recent payment |
# |
Jun 27th 2018, 09:19 |
neon1024 |
I guess a third, and little more obscure way, would be to use a subquery |
# |
Jun 27th 2018, 09:18 |
neon1024 |
In which case, if you don’t want that, you’ll want to use `matching()` to filter the `Mtypes` by the associated `Finances` |
# |
Jun 27th 2018, 09:18 |
neon1024 |
If you move the conditions into the contain, you can still return Mtypes though, but the associated Finances could be `null` |
# |
Jun 27th 2018, 09:17 |
neon1024 |
@itamer I would suspect that `MTypes` hasMany `Finances`, so the contain would do a separate query, to get the multiples. So you will need to put your filtering of the `Finances` table into the contain using a closure, or use `matching()` |
# |
Jun 27th 2018, 09:11 |
slackebot4 |
->order(['Finances.enddate' => 'DESC']) ->first() ->name; it falls over with SELECT Mtypes.name AS `Mtypes__name` FROM mtypes Mtypes WHERE (Finances.membership_id = :c0 AND Finances.created < :c1) ORDER BY Finances.enddate DESC LIMIT 1 because it's left out the Finances table and ignored the ->contain() Should I be querying the Finance model or is there something else missing? |
# |
Jun 27th 2018, 09:11 |
itamer |
Hi, probably a similar type of problem, maintaining some code that I suspect hasn't worked since it was upgraded from Cake1 to Cake3. Mtypes (Membership types) have many Finances (payments) - set up in the Table If I call $data['mtype'] = $this->Mtypes->find() ->select(['name']) ->contain(['Finances']) ->where(['Finances.membership_id'=>$data['membership_id'], 'Finances.created <' => $data['created']]) |
# |
Jun 27th 2018, 08:39 |
neon1024 |
etc |
# |
Jun 27th 2018, 08:39 |
neon1024 |
`$this->Contents->Parent->find()` |
# |
Jun 27th 2018, 08:39 |
neon1024 |
`$this->Parent->find()` `$this->Children->find()` `$this->Contents->find()` |
# |
Jun 27th 2018, 08:38 |
neon1024 |
So these are all now valid |
# |
Jun 27th 2018, 08:37 |
neon1024 |
https://ghostbin.com/paste/ob7fs |
# |
Jun 27th 2018, 08:36 |
neon1024 |
All defined in the ContentsTable |
# |
Jun 27th 2018, 08:36 |
neon1024 |
Where Parent and Children are aliases of Contents |
# |
Jun 27th 2018, 08:36 |
neon1024 |
Where Contents belongsTo Parent, and Content hasMany Children |
# |
Jun 27th 2018, 08:36 |
neon1024 |
$this->Contents->find()->contain([‘Parent’, ‘Children’]); |
# |
Jun 27th 2018, 08:35 |
neon1024 |
If you’ve aliased `SomeModel2` as `Parent` then use `Parent` |
# |
Jun 27th 2018, 08:35 |
magiq_ |
*it doesn't know |
# |
Jun 27th 2018, 08:35 |
magiq_ |
but my query not working since it not know what the fck it SomeModel2.Parent |
# |
Jun 27th 2018, 08:35 |
magiq_ |
SomeModel belongsTo SomeModel2 and SomeModel2 belongsTo SomeModel2 and has alias Parent |
# |
Jun 27th 2018, 08:34 |
neon1024 |
But I will try |
# |
Jun 27th 2018, 08:34 |
neon1024 |
Not sure I have the energy this early in the morning to write your code for you |
# |
Jun 27th 2018, 08:34 |
magiq_ |
can you write example |
# |
Jun 27th 2018, 08:33 |
neon1024 |
Just query them using the alias, as you would a regular model |
# |
Jun 27th 2018, 08:33 |
magiq_ |
I already define association, but I cant to query to relation of related models |
# |
Jun 27th 2018, 08:33 |
neon1024 |
The same way you would if you were creating a thread in a single model |
# |
Jun 27th 2018, 08:32 |
neon1024 |
Well you’d alias the models |
# |
Jun 27th 2018, 08:32 |
neon1024 |
Also, I would suggest that in fact it’s not related to itsself, as the association is in the parent. If I understand you correctly |
# |
Jun 27th 2018, 08:32 |
magiq_ |
how to $query->where(['SomeModel2.Parent.Type' => 1']) |
# |
Jun 27th 2018, 08:32 |
slackebot4 |
!ruleone |
# |
Jun 27th 2018, 08:32 |
slackebot4 |
Command sent from Slack by neon1024: |
# |
Jun 27th 2018, 08:31 |
magiq_ |
$query = $this->SomeModel() ... This model has relation to another model for example SomeModel2, and SomeModel2 has relation to itself through parent |
# |
Jun 27th 2018, 08:30 |
magiq_ |
I got $this->SomeModel |
# |
Jun 27th 2018, 08:30 |
neon1024 |
Morning all |