Answer the question
In order to leave comments, you need to log in
How to eager load ONLY an array of ids from a link table?
There are Job and JobCategory models. Multiple JobCategory can be associated with one Job. Their connection is implemented through a link table, and the method in the Job class looks like this:
public function getCategories() {
return $this->hasMany(JobCategory::className(), ['id' => 'category_id'])
->viaTable(JobToCategoryLink::tableName(), ['job_id' => 'id']);
}
Job::find()
->joinWith(['categories'])
public function getCategoryIdList() {
return $this->hasMany(JobToCategoryLink::tableName(), ['job_id' => 'id'])
->addSelect(['category_id']);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question