Eager loading of array of IDs

I have models Job and JobCategory. One Job has linked to many JobCategory (1 or more). The link is provided by junction table. The relation getter looks like this:

public function getCategories() {

    return $this->hasMany(JobCategory::className(), ['id' => 'category_id'])

      ->viaTable(JobToCategoryLink::tableName(), ['job_id' => 'id']);


Thus, I can do eage loading of all categories linked to a job:



This involves memory consumption, because the framework create array of JobCategory for each Job, and each of that array of JobCategory is unique object, dispite the fact these objects may represent the same data row.

I have not much JobCategory entries, so I decided to cache them into the redis, wrote JobCategoryRepository which has fetchAll($idList) method and returns JobCategory instances by list of id.

The question is: how to implement eager loading of CATEGORY ID ARRAY ONLY related to a Job? My first idea was something like that:

public function getCategoryIdList() {

    return $this->hasMany(JobToCategoryLink::tableName(), ['job_id' => 'id'])



and call it ->joinWith([‘categoryIdList’]), but in this case ‘categoryIdList’ relation is ALWAYS empty array. My second idea was return Query object with custom ‘select’ statemenet, but relations are quering by calling ->one or ->all in case relation query has ‘multiple’ parameter set to true. I need ->column.

Any ideas?

Hi katoomba,

I think you are right. But I don’t think it’s a big problem. I would not mind it at all.

I’m not sure what use case you have in mind, but I think this is an overkill. The performance improvement might be negligible even if you get it, and it won’t pay the cost.