K
K
Korvin32018-11-26 11:14:53
Yii
Korvin3, 2018-11-26 11:14:53

GridView Yii2: how to get all records even if no field is specified?

There are user(id, name), property(id, name) and user_property(id, user_id, property_id, value) tables. The user_property table stores user properties. If the property property.id=1 belongs to the user user.id=1, then the user_property table will have an entry (user_id=1, property=1, value=*some value*). If the property is not inherent to the user, then there will be no entry about it in the user_property table at all. I think there is such a pattern with its own name, I will be grateful if someone tells me. In the User model there is a link for each property, in the search model I pull up properties with $query->joinWith('propertyName propertyName') and then $query->andFilterWhere(['like', 'propertyName.value, $this-> propertyNameValue]). The gridView lists the attributes that I want to display. Everything is displayed, sorted, the search works, but, if one of the properties listed in the gridView is not inherent to the given User, then it will not be displayed, even if sorting/search by this property has not been specified. How to fix it?
In SQL it looks like this:
SELECT COUNT(*) FROM `user` LEFT JOIN `user_property` `propertyName` ON `user`.`id` = `propertyName`.`user_id` WHERE `propertyName`.`property_id`=1;
SELECT * FROM `user_property` `propertyName` WHERE (`propertyName`.`marker_id`=1) AND (`user_id` IN ('1', '2', '3'));
That is, I want to see the user in the gridView if he does not have an entry in the user_property with the corresponding property_id, which is in the gridView definition. Just so that the user has false/0/not set etc in the grid for this property.
PS: the real subject area is somewhat different, made it easier to understand by replacing it with user and property. I couldn't formulate a Google query that would give me the answer I needed.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Korvin3, 2018-11-26
@Korvin3

I asked it myself - I'll answer it myself :)
The error was in the User model. Where was used in the links, but onCondition was needed. Then the sql looks like this:
SELECT COUNT(*) FROM `user` LEFT JOIN `user_property` `propertyName` ON `user`.`id` = `propertyName`.`user_id` AND `propertyName`.`property_id`=1;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question