0

I have 3 models: Image, Company and File. So if we look through Company model, we have:

/**
* @return \yii\db\ActiveQuery
*/
public function getImages()
{
    return $this->hasMany('galleries\models\Image', ['id' => 'image_id'])
        ->viaTable('{{%companies_has_images}}', ['company_id' => 'id']);
}

public function extraFields()
{
    return ['images'];
}

now an Image model:

    /**
* @return \yii\db\ActiveQuery
*/
public function getFile()
{
    return $this->hasOne('app\models\File', ['id' => 'file_id']);
}

public function extraFields()
{
    return ['file'];
}

So here is the question, how can i get images with correct files in getImages() in the Company model?

rkalita
  • 575
  • 4
  • 16
  • 1
    Just use the relations? `foreach ($company->images as $image) $file = $image->file;` Your question is a somewhat unclear. – Blizz May 26 '15 at 05:51
  • Thank you, i was needed something like public function getFileImage() { foreach ($this->images as $image) { $files[] = $image->file; } return $files; } – rkalita May 26 '15 at 06:58
  • If that is the information you are looking for, I'll write it in an answer so you can mark the question resolved. – Blizz May 26 '15 at 06:59

1 Answers1

1

You'll have to fetch the images first and then provide an extra getter function to return the files:

public function getImageFiles() 
{
    $files = [];
    foreach ($this->images as $image)
       $files[] = $image->file;
    return $files;
}
Blizz
  • 8,082
  • 2
  • 33
  • 53