bikash.bilz
bikash.bilz

Reputation: 821

Alternative of using **joinType** for hasMany and BelongsToMany model

I want to get results of left side model/table, if and only if the condition satisfy in the right/contain model for hasMany and BelongsToMany association.

As per my knowledge, joinType is not available for hasMany and BelongsToMany association, so that I can't use InnerJoin...

This is my query,

$this->loadModel('Logins');
        $x = $this->Logins->find('all', [
            'fields' => ['id', 'user_name'],
            'contain' => [
                'Sites' => ['conditions' => ['site_id' => 10]]
            ],
        ]);
        pr($x->enableHydration(false)->toArray());
        die;

And This is the result,

[0] => Array
        (
            [id] => 13
            [user_name] => Mal123
            [sites] => Array
                (
                )

        )

    [1] => Array
        (
            [id] => 14
            [user_name] => rrc
            [sites] => Array
                (
                    [0] => Array
                        (
                            [id] => 10
                            [site_name] => Smelter
                            [site_location] => NA
                            [client_id] => 1
                            [site_status_id] => 1
                            [_joinData] => Array
                                (
                                    [id] => 15
                                    [login_id] => 14
                                    [site_id] => 10
                                )

                        )

                )

        )

QUESTIONS:-

  1. Why there is no option for 'joinType' for hasMany and BelongsToMany associations?
  2. How can I archive the same? I want to get only the 1'st one not the 0'th one

Upvotes: 0

Views: 118

Answers (0)

Related Questions