I had asked this in the Slack channel and got some help and general guidelines.
I’m using a $many_many to attach images to a page. I didn’t realize that $owns doesn’t work on many_many relations and only has_one. So I’m looking at having the images published when the page is. I didn’t create its own dataobject because I don’t need any sorting, titles, metadata etc, just simple 3-5 images attached to this pagetype so was looking to keep things simple.
The below code was linked to me that works for has_one images but I don’t know how to get it working with my many_many as I’m guessing I have to loop/foreach over the photos and publish each separately.
public function onAfterWrite()
{
parent::onAfterWrite();
if ($this->Image()->exists() && !$this->Image()->isPublished())
{
$this->Image()->doPublish();
}
}
In the example you can see it’s still a many_many, you’re just manually specifying the join table. Supporter in the example is like Image in your case, so you would set the from and through on your Page and set up an additional DataObject as the join (through) table. You could skip the $belongs_many_many specificaiton on Image if you won’t be accessing the relation from that side. You would also need to add an $owns spec on your Page for the Image relation.
I haven’t tried this for myself by the way, so hopefully the docs are right but they do say on that page that you can have a many_many relation with full support of the ownership API, so don’t lose hope just yet.
I’m a front end design guy
I was too when I started out with SilverStripe. Just you wait, soon you’ll be calling yourself a “full stack developer” and cringing a little like me hehe
Okay if I’m going to have to create another DataObject to join tables with many_many_through I might as well use the bulk upload module that I linked above so I can also use sortablegridfield extension and get sorting as well.
Two things. First, that still doesn’t publish the images on page save, and two, it doesn’t remove the gridfield add button as I wouldn’t need it with the bulk uploader module.
Yes my dataobject I am using for the photo owns the image.
Going back to your original example, have you tried just looping over the many_many relation and publishing? So on your Page you would have something like:
private static $many_many = [
'Photos' => Image::class
];
public function onAfterWrite()
{
parent::onAfterWrite();
foreach ($this->Photos() as $photo) {
if (!$photo->isPublished()) {
$photo->publishSingle();
}
}
}
So apparently it does work with $many_many relationships. I had asked in Slack and most said it didn’t work but there was 1 person who believed it did. I had tried and it wasn’t working, but I may have forgot to run dev/build though I was pretty sure I did. So I assumed it didn’t work.
A few days later I revisited the question in there and it does work and I have it working now.
And if anybody reading needs an easy way to manage sorting the uploads as well, I was pointed to this module which is simple and great.
So just create a $many_many with that new field and add $owns to the page and you’re good to go.