Home > Backend Development > PHP Tutorial > Can't Yii2 joint table query check individual fields?

Can't Yii2 joint table query check individual fields?

PHP中文网
Release: 2023-03-01 06:18:01
Original
2302 people have browsed it

When tables are not joined, individual fields can be checked.

$user->find()->select(['userid', 'username'])->asArray()->all();
Copy after login
Copy after login

Then SQL is also normal

select userid, username from ...
Copy after login
Copy after login

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();
Copy after login
Copy after login

I looked at the query SQL and it turned out to be

select * from ....
Copy after login
Copy after login

What a scam? Or is there something wrong with the method I'm using? ?

Reply content:

When the tables are not connected, you can check individual fields.

$user->find()->select(['userid', 'username'])->asArray()->all();
Copy after login
Copy after login

Then SQL is also normal

select userid, username from ...
Copy after login
Copy after login

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();
Copy after login
Copy after login

I looked at the query SQL and it turned out to be

select * from ....
Copy after login
Copy after login

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();
Copy after login

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)!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template