Home >Backend Development >PHP Tutorial >Can't Yii2 joint table query check individual fields?
When tables are not joined, individual fields can be checked.
$user->find()->select(['userid', 'username'])->asArray()->all();
Then SQL is also normal
select userid, username from ...
But when joining tables. . .
$user->find() ->joinWith([ 'account' => function ($object) { $object->select(['account_name', 'account_level', 'account_status']); }, 'bank' => function ($object) { $object->select(['bank_name', 'bank_province', 'bank_branch', 'bank_account']); } ]) ->asArray() ->all();
I looked at the query SQL and it turned out to be
select * from ....
What a scam? Or is there something wrong with the method I'm using? ?
When the tables are not connected, you can check individual fields.
$user->find()->select(['userid', 'username'])->asArray()->all();
Then SQL is also normal
select userid, username from ...
But when joining tables. . .
$user->find() ->joinWith([ 'account' => function ($object) { $object->select(['account_name', 'account_level', 'account_status']); }, 'bank' => function ($object) { $object->select(['bank_name', 'bank_province', 'bank_branch', 'bank_account']); } ]) ->asArray() ->all();
I looked at the query SQL and it turned out to be
select * from ....
What a scam? Or is there something wrong with the method I'm using? ?
You should write select outside joinWith:
$user->find()->select(['account_name', 'account_level', 'account_status', 'bank_name', 'bank_province', 'bank_branch', 'bank_account']) ->joinWith(['account', 'bank']) ->asArray() ->all();
Of course it will be select *
Because your operation only specifies the select field when querying the related table.
So I still specify it individually as you did Just select
The above is that Yii2 joint table query cannot check individual fields? For more related content, please pay attention to the PHP Chinese website (www.php.cn)!