Nesting a Class in the Page Controller so it can be viewed in the Page Template

Hey guys, probably a pretty simple one! (if you’re wondering about all the naming, this one is for a stand up comedy website).

I’m using SS v4 and have set up the Class Comedian with data inputs and a ModelAdmin extension ManageComedians to add/edit them in the CMS. All that is working. But I can’t get the whole list of Comedians I’ve added to show on the page ComedianHolder.ss using $ViewComics. The controller can’t access the Comedians class. I’m getting the error: [Emergency] Uncaught Error: Class ‘SilverStripe\WellingtonComedy\Comedians’ not found

The following 3 pages are located in app/src:

Comedian.php:

use SilverStripe\ORM\DataObject;
use SilverStripe\Assets\Image;
use SilverStripe\AssetAdmin\Forms\UploadField;

class Comedian extends DataObject 

{
    private static $db = [
        'Name' => 'Varchar',
        'Blurb' => 'Varchar',
        'Profile' => 'Text'
    ];

       private static $has_one = [
        'Photo' => Image::class,
    ];
        private static $owns = [
        'Photo'
    ];

        private static $searchable_fields = [
        'Name'
    ];


}

ManageComedians.php

use SilverStripe\Admin\ModelAdmin;

class ManageComedians extends ModelAdmin 
{

    private static $managed_models = [
        Comedian::class
    ];

    private static $url_segment = 'manage-comedians';
    private static $menu_title = 'Manage Comedians';
    private static $menu_icon_class = 'font-icon-happy';
}

ComedianHolder.php

namespace SilverStripe\WellingtonComedy;

use Page;   
use PageController;   

class ComedianHolder extends Page 
{
    private static $has_many = [
    'Comedians' => Comedian::class,
	];

    private static $owns = [
    'Comedians'
    ];
}

class ComedianHolderController extends PageController 
{
	public function ViewComics() 
  { 
    return Comedians::get()
            ->limit(6);
  } 

}

If it helps here’s the project on Github

Thanks heaps :slight_smile:

It’s not working because you haven’t set the same namespace on the Comedians class as you have for the ComedianHolder class. You would either need to add the namespace to the other class, or use \Comedians::class etc in your code.

Hope this helps!

It’s not working because you’re using the wrong classname.

The dataobject class is called ‘Comedian’ and you’re calling Comedians::get() (plural) in your ViewComics() method.

A few observations:

Since you’ve gone to the effort of making a relation from the dataobject to the page, you might as well use that relation for the get…

Currently, you’re getting any old comedian records from the database, rather than the ones which are connected to the page in question, you could use:

return $this->Comedians()->limit(6);

You can move the entire ViewComics() method into your ComedianHolder class… it doesn’t need to be in the controller.

On the note of the controller, it’s good practice to keep one class per file, since this helps with autoloading, etc. If that’s all you have in the controller class, you can put your dataobject logic in the page and ditch that controller… but otherwise, I’d move it to a new file of it’s own.

Thanks so much guys, that’s making sense now.

Once this site is done I’ll shout you guys some free tickets to a stand up show in wellington :slight_smile:

No worries! glad you’ve managed to sort it out.

A kind offer of you, sadly Wellington is a bit of a journey for me, I’m in the UK!