Yii2 Gridview left Join Inverse

0

I am using to make a query:

Usuarios
------------
juan
pedro
oscar

Tarjetas
------------
juan 123
pedro 987
oscar 657
oscar 987

I mean I have less users than cards , one has 2 cards. If I make this query:

select * from Usuarios leftJoin Tarjetas On idusuario = idUsuarioTarjeta

He returns everything to me; that is:

juan 123
pedro 987
oscar 657
oscar 987

But if I do that same in yii2 already with the relationships in the models and everything:

find()->joinwith('tarjetas');

The gridview only returns this:

juan 123
pedro 987
oscar 657

If I make a find()->joinwith('tarjetas')->select('nombreUsuario, interbancariaCuenta'); I get what I want, but the gridview no longer recognizes the card values, that is, the gridview no longer recognizes tarjetas.interbancariaCuenta .

I have already changed the ratio of hasMany to hasOne , but still:

juan  nodefinido
pedro nodefinido
oscar nodefinido
oscar nodefinido

I know I can do the search not inverted, cards- > joinleft to Users , and we would have what I want to do, but as I plan to join it with other tables, I'll use the inverted one so that I threw n number of times the users and each of the accounts or cards.

It should be noted that if I do a select direct in mysql it does it without any problem and gives me all the values I want, it's just the issue with the yii2

How can I resolve this situation?

    
asked by Sergio Aldo Mejía Rodríguez 02.06.2017 в 23:54
source

1 answer

0

Well, I always always respond only ...

How I did not manage to do it using the yii models. just use a sqlDataProvider by hand.

$totalCount = Yii::$app->db->createCommand('SELECT COUNT(*) FROM posts WHERE publish=:publish', [':publish' => 1])
            ->queryScalar();

$dataProvider = new SqlDataProvider([
    'sql' => 'SELECT * FROM posts WHERE publish=:publish',
    'params' => [':publish' => 1],
    'totalCount' => $totalCount,
    //'sort' =>false, to remove the table header sorting
    'sort' => [
        'attributes' => [
            'title' => [
                'asc' => ['title' => SORT_ASC],
                'desc' => ['title' => SORT_DESC],
                'default' => SORT_DESC,
                'label' => 'Post Title',
            ],
            'author' => [
                'asc' => ['author' => SORT_ASC],
                'desc' => ['author' => SORT_DESC],
                'default' => SORT_DESC,
                'label' => 'Name',
            ],
            'created_on'
        ],
    ],
    'pagination' => [
        'pageSize' => 10,
    ],
]);
    
answered by 05.06.2017 в 17:46