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

Can't Yii2 joint table query check individual fields?

PHP中文网
PHP中文网Original
2017-03-30 13:41:522303browse

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? ?

Reply content:

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

Statement:
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