Gallery with many_many relation on image

silverstripe-4
Tags: #<Tag:0x00007f10d2ebadb8>

#1

I am trying to silverstripe framework without cms, have create Gallery model as mentioned below with many_many to Image, Category. And I am able print to count and title of Categories, but with Images returns null; does not print uploaded images .

// model

<?php

namespace App\Model;

use SilverStripe\ORM\DataObject;
use SilverStripe\Assets\Image;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\CheckboxSetField;
use SilverStripe\AssetAdmin\Forms\UploadField;
use App\Model\Category;

class Gallery extends DataObject{

    private static $db = [
        'Name' => 'Varchar',
    ];
	
    //...
    private static $many_many = [
        'Images' => Image::class,
		'Categories' => Category::class
    ];

	
	private static $table_name = 'Gallery';	
	
    public function getCMSFields()
    {
        $fields = FieldList::create(
            TextField::create('Name'),            
            $uploader = UploadField::create('Images'),
			CheckboxSetField::create(
				'Categories',
				'Selected categories',
				Category::get()->map('ID','Title')
			)			
        );

        //$uploader->setFolderName('region-photos');
        $uploader->getValidator()->setAllowedExtensions(['png','gif','jpeg','jpg']);

		
        return $fields;
    }	
}

// template

<h2>Gallery - $Gallery.Name <h2>

$Gallery.Images.Count images<br>
$Gallery.Categories.Count categories<br> 


<% with $Gallery %>	
	
	<% loop $Images %>
		$Me					
	<% end_loop %>

	<% loop $Categories %>
		<h5>$Title</h5>
	<% end_loop %>
	
<% end_with %>

// controller

<?php

namespace App\Control;

use SilverStripe\Control\Controller;
use App\Model\Gallery;
use SilverStripe\Dev\Debug;
use SilverStripe\Dev\Backtrace;


class GalleryController extends Controller{

    private static $allowed_actions = array(
        'index'
    );

	
	public function index(){
        return $this->renderWith(array('Gallery', 'Page'));	
	}
	
	public function Gallery(){
		$gallery = Gallery::get()->first();
		return $gallery;		
	}

	public function Count(){
		$gallery = Gallery::get()->first();
		Debug::show(($gallery->Images())->count());
		return $gallery->Images()->count();		
	}
	
	
}

#2

You probably need to add an extension most likely to the image class or extend it so that it states the image belongs_many_many to the Gallery Class.

You should be able to do this with a DataExtension and enable it via your YAML config.

This is 3.x documentation but should still apply. https://docs.silverstripe.org/en/3/developer_guides/forms/field_types/uploadfield/#using-the-uploadfield-in-a-frontend-form


#3

I figured out that by default the uploaded images are in draft mode, Is there way to make the image public by default.


#4

You can use the $owns directive for that (in your dataobject class):

private static $owns = [
  'Images'
];

(Don’t forget to run a dev/build)

When you save the dataobject, the related images should be published automatically.