This article describes in detail how to use the yii2 framework to perform table join queries. It also describes how to query one or more fields. When the table is not joined, you can check individual fields.
$user->find()->select(['userid', 'username'])->asArray()->all();
Then SQL is normal.
select userid, username from ...
But when you join the table...
$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 read the query SQL statement.
select * from ....
Where is it? Or is there a problem with my method ??
Reply content:
When the table is not joined, you can check individual fields.
$user->find()->select(['userid', 'username'])->asArray()->all();
Then SQL is normal.
select userid, username from ...
But when you join the table...
$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 read the query SQL statement.
select * from ....
Where is it? Or is there a problem with my method ??
You should write the select statement 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 yesselect * Because your operation only specifies the select field when querying the associated table.
So it's better to specify the select statement as per your individual statement.
Which of the following fields cannot be queried in the Yii2 join table query? For more information, see The PHP Chinese website (www.php1.cn )!