0

I execute the following request:

$agPoi = $this->Agpois->get($agPoiId, [
    'contain' => [
        'Agpoititles.Agthemelanguages' => function ($q) use ($languageId) {
            return $q->where([
                'language_id' => $languageId,
            ]);

        }
    ]
]);

Surprisingly, it returns me all agpoititles with only one containing a not empty aghtemelanguage object for which 'language_id' => $languageId.
But I'm not interested by agpoititles not containing an agthemelanguage object.
How to modify the request to only the ones I want?

EDIT

Agpois hasMany Agpoititles
AgpoiTitles belongsTo Agthemelanguages
Agthemelanguages hasOne Agpoititles

SO I expect to get agPoi with THE Agpoititles for which its agthemelanguage->language_id = $languageId

Edit 2

So I tried to use matching() and it works if you pay attention to look for the result in _matchingData as it's explained in the doc ;).

$agPoi = $this->Agpois->find()
              ->contain(['Agpoititles.Agthemelanguages'])
              ->matching('Agpoititles.Agthemelanguages', function ($q) use ($languageId, $poiId) {
                   return $q->where([
                      'Agpois.id' => $poiId,
                      'language_id' => $languageId
                      ]);
                  }
              )
              ->first();
Cœur
  • 37,241
  • 25
  • 195
  • 267
fralbo
  • 2,534
  • 4
  • 41
  • 73
  • Possible duplicate of [find on associated model's condition CakePHP 3.0](http://stackoverflow.com/questions/26799094/find-on-associated-models-condition-cakephp-3-0) – ndm Feb 18 '16 at 14:08
  • please explain the relationships between the models – arilia Feb 18 '16 at 14:08
  • @ndm I think it's not relate to my problem as I find what I need but Cake returns more than what I need. – fralbo Feb 18 '16 at 14:28
  • @arilia I edit my post. – fralbo Feb 18 '16 at 14:28
  • 1
    It's the same problem, just for a containment rather than the main table... give it a try. – ndm Feb 18 '16 at 14:49
  • @ndm but I'm not a belongsToMany association so I don't see why to use matching(). I don't really understand what you try to tell me. Sorry. – fralbo Feb 18 '16 at 16:46
  • The type of association doesn't matter. You want to filter by an association (as far as I understood), and that's what `Query::matching()` (and `Query::innerJoinWith()`) is there for. – ndm Feb 18 '16 at 16:52
  • @ndm: ah ok. I look at it. – fralbo Feb 18 '16 at 17:18
  • @ndm I edited my post. It still doesn't work :(. – fralbo Feb 18 '16 at 17:44
  • @ndm, sorry, I just didn't look in the right area in the results set. – fralbo Feb 18 '16 at 18:13

0 Answers0