Answer the question
In order to leave comments, you need to log in
Adding arrays to the server response. REST on Yii2?
There is a page with vacancies, which displays vacancies by employer. We need to return a bunch of jobs and information about the employer from the server to display the name of the company so that people can see who owns these jobs. How to do it?
There is an ActiveDataProvider:
$vacancies = new ArrayDataProvider([
'allModels' => Vacancy::find()->where(['creator' => $creator]),
'pagination' => [
'pageSize' => 30
]
]);
return $vacancies;
$employer = Profile::find()->select('company')->where(['user' => $creator])->one();
Answer the question
In order to leave comments, you need to log in
Good evening.
You need to set up links between models.
It looks something like this:
In the profile table, the id of the record is taken, in this case "id" and is looked up in the job table, which should contain "id_company" corresponding to the id of the company profile.
public function getVacancy()
{
return $this->hasMany(Vacancy::class, ['id_company' => 'id']); // у одного профиля может быть много вакансий
}
public function getProfile()
{
return $this->hasOne(Profile::class, ['id' => 'id_company']); // у одной вакансии может быть только один профиль.
}
$vacancies = new ArrayDataProvider([
'allModels' => Vacancy::find()->with('profile')->where(['creator' => $creator]),
'pagination' => [
'pageSize' => 30
]
]);
return $vacancies;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question