CakePHP 3 - conditions on deep assocations with reference to parent id

71 views Asked by At

I am trying to find from table "spots" a single instance, with a set of deep associations. Spots have one template, which has multiple "fields", which in turn have multiple "fieldentries". A "fieldentry" however, is also associated with the particular "spot". I hope this makes sense.

My finder looks like this:

public function findFull(Query $query, array $options)
{
    $query->contain([
        'Template' => [
            'Fields' => [
                    'Fieldentry' => function($q){
                        return $q->matching('Spots.id');
                    }
                ]
            ]
    ]);
    return $query;
}

Calling this gives me all "fieldentries" associated with the field. But I need that selection restricted to just the one that is linked to the spot in question.

I am fairly sure that the problem is with the matching function, but I can't figure out how to do that properly. I can make it work by passing the spot id by the options argument, but I would like to be able to do this without knowing the id beforehand (using another finder with different conditions on the spot). So I was hoping for a more "automagic" solution. Is this not possible?

0

There are 0 answers