Answer the question
In order to leave comments, you need to log in
Yii2: how to add data from another table to AR?
There are two tables: user containing the list of users and film containing the list of movies. The user has the ability to bookmark the movie. To do this, I add a third table , user_bookmark , which I use to link the user and film tables . In general, a standard bunch of many-to-many. In the application, this is implemented through ActiveRecord. There are no problems here.
But I also need to store the time when the user bookmarked the movie. To do this, I added the date_create field to the user_bookmark table , where I write this date. Now I need AR Film to have this field, or the ability to cheat on it.
Right now I just have a getDateCreate() method that pulls this date. But this is very suboptimal. For each movie you need to make a separate request. Tell me, how can I add a field from another table to AR? I will be grateful.
user user_bookmark film
==== ============= ====
id
id -------E user_id
film_id Э------- id
login name
date_create
Answer the question
In order to leave comments, you need to log in
1. create a getUserBookmarks link in the User model
2. create a UserBookmark model, use the getFilms link
from User in it like this:
you can add asArray() somewhere in the links to make the process easier.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question