A
A
Ann20162016-06-18 18:47:41
Yii
Ann2016, 2016-06-18 18:47:41

Yii2 relations and GridView?

Such a question, it is necessary to display a list of users with email and full name in the GridView with a minimum number of requests. Email in user, full name in profile.
In User I added a link like this

public function getProfile()
    {
        return $this->hasOne(Profile::className(), ['user_id' => 'id']);
    }

In profile
public function getUser()
    {
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }

CREATE TABLE `profile` (
`id_prof` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(32) NOT NULL,
`surname` varchar(32) NOT NULL,
`patronymic` varchar(32) NOT NULL,
`user_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id_prof`),
KEY `idx_user_id` (`user_id`),
CONSTRAINT `fk_user_profile_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE
) ENGINE =InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8 I
already tried to form models for the dataProvider with all queries, but it doesn't work out at night. Outputs data only if 'profile.name' is specified, which creates an additional query for each row.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Klyuev, 2016-06-18
@Ann2016

Read about with, around here www.yiiframework.com/doc-2.0/guide-db-active-recor...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question