Cannot I query individual fields in the Yii2 join table?

Source: Internet
Author: User
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 )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.