S
S
Sergey Beloventsev2016-05-09 07:23:21
Yii
Sergey Beloventsev, 2016-05-09 07:23:21

How to use data from two related tables?

good time of day question in the following there are two tables

serial=>id, name_serial, created_at;
films=>id, name_films, created_at,id_serial;

tables are connected to each other in models, and serial is also connected to the image Serial model
public function getFilms(){
        return $this->hasMany(Films::className(),['id_serial'=>'id']);
    }
public function getImages(){
        return $this->hasMany(Image::className(), [ 'id_serial'=>'id' ]);
    }

Films
public function getSerial()
{
    return $this->hasOne(Serial::className(), ['id'=>'id_serial']);
}

I need to display data from both tables by date (created_at) so that I can then use links, for example, so that when outputting Film, I can then get Image from Serial like this $films->serial->images and serial-> image serial->films

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Arutyunov, 2016-05-09
@Sergalas

Use joinWith() or with() when making a request.

$films = Films::find()
  ->joinWith('serials')
  ->orderBy(['films.created_at' => SORT_DESC])
  ->addOrderBy(['serials.created_at' => SORT_DESC])
  ->all();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question