This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

yokann15744
yokann15744

Hi,

I saved attachment files from a model and i need to access to this attachments through a child model. I have tried but it seems doesn't work due to the model namespace saved in the system_files table.

I have this class :

 class Product extends Model {
      //.......some code
      public $attachMany = [
        'images' => ['System\Models\File', 'order' => 'sort_order']
    ];
   //.......some code
}

and this one :

 class ProductVariable extends Product {
     //......some code
}

Is there a way to do it ?

Thank you.

Last updated

philipptempel
philipptempel

Generally yes, that would be possible. But not using October because of the way it handles this kind of attachMany. What you could to do circumvent this is create a relationship between your two models Product and ProductVariable such that a Product has one ProductVariable and a ProductVariable belongs to Product. This way you can just access the images using

$productVariable->product->images
yokann15744
yokann15744

Thank you but i think this solution is not very elegant in my case.

I made my day with this :

 class Product extends Model {
  //.......some code
  public $attachMany = [
    'images' => ['System\Models\File', 'order' => 'sort_order']
];
public function getMorphClass()
{
    return 'Acme\Plugin\Models\Product';
}
}

It will force the child class to use the namespace of its parent. All the relationship works fine with this.

Last updated

1-3 of 3

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.