BelongsToMany Pivot table Query


#1

Hello !

I need advice concern the best way to write Query with cakePhp for the following case

Table profiles
id name

Table businesses
id name

Table businesses_profiles
profile_id business_id

I would like to get all businesses related to loggedIn profile
So I writed this way

$businesses = $this->Businesses->find()->contain([
        'Profiles'
    ])->matching(
        'Profiles', function ($q){
            return $q->where([
                'BusinessesProfiles.profile_id' => 17
            ]);
    });

Correct ? Or best solution ? What’s your opinion ? Thanks !

Laurent.