Advanced Relation With Activerecord

5640

database.png

Can someone tell me what i am doing wrong ?

I have this code where i get result like this:

Problem is that I want to get code what will look like this.

Where should i put this $agentId so that it will return correct record. not first from table kv_value

[

0 => app\models\KvTable#1


(


    [relatedKvKeys] => null


    [fields] => null


    [yii\db\BaseActiveRecord:_attributes] => [


        'id' => 1


        'project_id' => 9


        'name' => 'test'


        'created_at' => '2014-05-31'


        'updated_at' => '2014-05-31'


    ]


    [yii\db\BaseActiveRecord:_oldAttributes] => [


        'id' => 1


        'project_id' => 9


        'name' => 'test'


        'created_at' => '2014-05-31'


        'updated_at' => '2014-05-31'


    ]

0 => app\models\KvKey#2

            (


                [yii\db\BaseActiveRecord:_attributes] => [


                    'id' => 1


                    'table_id' => 1


                    'name' => '111111111111111'


                    'type' => 'date'


                    'created_at' => '2014-05-31'


                    'updated_at' => '2014-05-31'


                ]


                [yii\db\BaseActiveRecord:_oldAttributes] => [


                    'id' => 1


                    'table_id' => 1


                    'name' => '111111111111111'


                    'type' => 'date'


                    'created_at' => '2014-05-31'


                    'updated_at' => '2014-05-31'


                ]


                [yii\db\BaseActiveRecord:_related] => [

[b] ‘kvValue’ => app\models\KvValue#3

                    (


                        [yii\db\BaseActiveRecord:_attributes] => [


                            'id' => null


                            [color="#FF0000"]'agent_id' => 1[/color]


                            'key_id' => 1


                            'value' => 'test'


                            'created_at' => '2014-06-02'


                            'updated_at' => '2014-06-02'


                        ]


                        [yii\db\BaseActiveRecord:_oldAttributes] => [


                            'id' => null


                            'agent_id' => 1


                            'key_id' => 1


                            'value' => 'test'


                            'created_at' => '2014-06-02'


                            'updated_at' => '2014-06-02'


                        ]


                        [yii\db\BaseActiveRecord:_related] => []


                        [yii\base\Model:_errors] => null


                        [yii\base\Model:_validators] => null


                        [yii\base\Model:_scenario] => 'default'


                        [yii\base\Component:_events] => []


                        [yii\base\Component:_behaviors] => []


                    )[/b]


                ]

]

$agentId = 2;

    $tables = KvTable::find()


        ->leftJoin('kv_key', ['kv_table.id' => 'kv_key.table_id'])


        ->leftJoin('kv_value', ['kv_value.key_id' => 'kv_key.id', 'kv_value.agent_id' => $agentId])


        ->with(['kvKeys', 'kvKeys.kvValue'])


        ->where(['kv_table.project_id' => 9])


        ->all();

$this->agentId = 2;

$tables = KvTable::find()

        ->with(['kvKeys', 'kvKeys.kvValue' => function($q) {


                    $q->where(['kv_value.agent_id' => $this->agentId]);


                }])


        ->where($condition)


        ->all();