Publish

Silverstripe Version: 4.1

Question:

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.

private static $many_many = [
  'Photos' => Image::class
];
public function onAfterWrite()
 {
   parent::onAfterWrite();
    if ($this->Image()->exists() && !$this->Image()->isPublished())
       {
	  $this->Image()->doPublish();
	}
 }

Hi Liam, actually DataObjects can own many_many items by using the through setting. Check this out: Relations between Records – SilverStripe Documentation

But that won’t work for my example will it? I’m using Image::class for my many_many so there is from or to.

I’m a front end design guy, so I had no idea SS4 was going to make something I thought was simple and worked in SS3 so complex.

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 :smile: 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

I don’t follow.

Is there any working code for onAfterWrite() as I still think that would be a more simple approach instead of writing new DataObjects.

Before posting here I Googled around and numerous other people are having this same issue. I had looked into GitHub - colymba/GridFieldBulkEditingTools: SilverStripe GridField Components set for bulk upload and bulk record edit, unlink & delete but I still couldn’t get that to publish on page save either and I don’t like the UI it ends up creating.

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.

My code is now

	$fields->addFieldToTab('Root.Photos',
    GridField::create(
      'Photos',
      'Photos',
      $this->Photos(),
      GridFieldConfig_RecordEditor::create()
      	->addComponent(new GridFieldSortableRows('SortOrder'))
				->addComponent(new \Colymba\BulkUpload\BulkUploader(null, null, true))
				->removeComponentsByType('GridFieldAddNewButton')
    )
  );

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.

Can anybody see why this isn’t working?

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();
        }
    }
}

Sorry, forgot to update this thread.

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.

1 Like