Even further, what if I want to get 10 users with newest profiles. So user do not need to have profile created, and I wan to get latest 10 with fresh profiles.
Does anyone have any clue what is the right way to do this ?
I assume that you use User controller and the action that give back the last 10 user is index and you api user model is in \api\modules\v1\models\
I assume also that you have an “active” column to know if the user is active and that you have a ‘creation_date’ column as well.
class UserController extends ActiveController {
public function actions() {
$actions = parent::actions();
$actions['index']['prepareDataProvider'] = [$this, 'prepareDataProvider'];
return $actions;
}
public function prepareDataProvider() {
$query = \api\modules\v1\models\User::find()
->where('active=1')
->limit(10)
->orderBy(['creation_date'=>SORT_DESC]);
return new \yii\data\ActiveDataProvider([
'query' => $query,
'pagination' => false, // just in case you have a pagination under 10 elements
]);
}