Answer the question
In order to leave comments, you need to log in
How to work with linked tables in Yii2 using ActiveRecord?
Hello!
I am learning the Yii2 framework, I got to work with the database.
I generate CRUD using gii, the findModel function appears in the controller, in which there is such a request: $model = Pages::findOne($id)
This line returns data from the table by ID.
I need to get data from a related table, I do this:
$model = Pages::find()
->leftJoin('content', '`content`.`page_id` = `pages`.`id`')
->where(['content.page_id' => $id])
->all()
Answer the question
In order to leave comments, you need to log in
Create an association with the getContents() model. After that, you can access the attributes of the associated table through the relationship: $model->getContents()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question