Has_many images

Silverstripe Version: 4.2

I have a DataObject:

class Slide extends DataObject

I want to attach many images, so I did:

private static $has_many = [‘Photos’=>Image::class];

After /dev/build run successfully on the backend page I get this error:

[Emergency] Uncaught Exception: No has_one found on class ‘SilverStripe\Assets\Image’, the has_many relation from ‘Slide’ to ‘SilverStripe\Assets\Image’ requires a has_one on ‘SilverStripe\Assets\Image’

How can I add the has_one?

Thx

If it’s just for the relation, you don’t need a full extension. You can add it directly in a yml config:

SilverStripe\Assets\Image:
  has_one:
    Slide: Slide

(If your ‘Slide’ dataobject is in a namespace ,which it hopefully is, then you’ll need the fully namespaced classname in the yml file)

https://docs.silverstripe.org/en/4/developer_guides/model/relations/#has-many

3 Likes

Nice didn‘t know that.

Thank you, solved it now.

Just wanted to add that a many_many may be better here. Using has_many makes each Image able to be tied to only one Slide which could be limiting.

1 Like