A
A
AlexSer2019-11-05 09:07:52
Yii
AlexSer, 2019-11-05 09:07:52

How do you display items by id through a linked table in Yii2?

I have an intermediate table.
Let's say we have a relationship between three tables.

tovar             zakaz_tovar            user
id                   id                           id
title               id_tovar                  name
                     id_user 

 public function getUser(){
        return $this->hasMany(User:className(), ['id' => 'id_user'])
            ->viaTable('zakaz_tovar', ['id_tovar' => 'id']);

    }

If I make a request $model=Product::findOne($id);
and then through the loop I try to display user, then it displays not in the order of the entry in zakaz_product, but by user id. How to make records displayed by id from the intermediate table?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2019-11-05
@AlexSer

Add to condition.->orderBy('id ASC');

public function getUser(){
        return $this->hasMany(User:className(), ['id' => 'id_user'])
            ->viaTable('zakaz_tovar', ['id_tovar' => 'id'])->orderBy('id_tovar ASC');
    }

public function getUser(){
        return $this->hasMany(User:className(), ['id' => 'id_user'])
            ->viaTable('zakaz_tovar', ['id_tovar' => 'id'])->orderBy('id DESC');
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question