0

I want to get associated only if the second level associated data respect the given condition.

The query will maybe better explain what I try to do.

$selSite = $this->Sites->get($selSiteId, [
    'contain' => [
        'Agplans.Products' => function ($q) {
            return $q
            ->where([
                'Products.type' => 'hosting',
            ]);
        }
    ]
]);

So I expect an agplan only if its associated product matches the condition.

But the result is:

'agplans' => [
    (int) 0 => object(App\Model\Entity\Agplan) {

        'id' => (int) 20,
        'product_id' => (int) 4,
        'product' => null,
        ...,

    },
    (int) 1 => object(App\Model\Entity\Agplan) {

        'id' => (int) 21,
        'site_id' => (int) 64,
        'product_id' => (int) 75,
        'product' => object(App\Model\Entity\Product) {

            'id' => (int) 75,
            ...,

        },
        ...,

    }
],

My problem here is to get agplan[0] with a product => null. It's not what I understood from the doc. How to get the agplan with 'product' => object(App\Model\Entity\Product) only?

ndm
  • 59,784
  • 9
  • 71
  • 110
fralbo
  • 2,534
  • 4
  • 41
  • 73
  • Sounds like **http://stackoverflow.com/questions/26799094/how-to-filter-by-conditions-for-associated-models** to me. – ndm Mar 30 '17 at 13:23
  • @ndm I don't think it's the same problem. Here I don't want to get few sites for which associated matches the condition, I only `get` one specific site. I just want here to filter out its associated. What the [doc](https://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#passing-conditions-to-contain) explains telling `It is also possible to restrict **deeply-nested associations** using the dot notation:`. I don't see what I didn't understand here. – fralbo Mar 30 '17 at 13:42

1 Answers1

1

So thanks to what @ndm pointed me out I understood that I have to build my query like that:

$selSite = $this->Sites->get($selSiteId, [
    'contain' => [
        'Agplans.Products',
        'Agplans' => function ($q) {
            return $q
            ->matching('Products', function ($q) {
                return $q
                ->where([
                    'type' => 'hosting',
                ]);
            });
        }
    ]
]);

That way, I only get agplans matching the given product. Anyway, I find the CakePHP doc not clear on the contain feature about restricting associated.

fralbo
  • 2,534
  • 4
  • 41
  • 73
  • I guess it could possibly be a little confusing for people that aren't that familiar with containments yet, so you may want to [**report this as an issue**](https://github.com/cakephp/docs/issues), a small hint about what "_restrict deeply-nested associations_" refers to, and that it shouldn't be confused with filtering by associated data surely wouldn't hurt. – ndm Mar 30 '17 at 14:22
  • @ndm, Is it to be considered as an issue? I currently proposed to clarify the doc on that point. – fralbo Mar 30 '17 at 14:43
  • Well, "issue" certainly can mean a lot of different things based on the context, I just wanted to refer to GitHub tickets, which are called "issues". I should maybe choose better wording in the future. – ndm Mar 30 '17 at 14:51
  • @ndm ok I do it. – fralbo Mar 30 '17 at 15:25