Hi,
I’m getting all the posts a user want to “read later” to show them in a list view. I want to show them in the order the user marked them “to read later”. I can show this list, but I can’t order it as I want.
I have a read_later table (relation many-to-many table between user and post).
this many-to-many table has a user_id column, a post_column AND an action_date column
I want my dataprovider to be ordre by this action_date.
Here how I do :
in my PostController :
public function actionWillread($user_id)
{
....
$dataProvider = Post::getWillreadPostsOfUser($user);
....
In the post model, I have :
static function getWillreadPostsOfUser($user)
{
$dataProvider = new ActiveDataProvider([
'query' => $user->getWillreadPosts()
]);
return $dataProvider;
}
In the user model, I have :
public function getWillreads()
{
//
// ORDER BY IS DEFINED HERE !!!
//
return $this->hasMany(WillReads::className(), ['user_id' => 'id'])->orderBy('action_date');
}
public function getWillreadPosts()
{
return $this->hasMany(Post::className(), ['id' => 'post_id' ])
->via('willreads');
}
It seems that ActiveDataProvider doesn’t preserve the order of the query.
Also, it seems the query doesn’t return the read_later.action_date column.
what to do ?