Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Extending sub classes of DataObject


Go to End


2 Posts   1230 Views

Avatar
ContentBloom

Community Member, 5 Posts

1 November 2011 at 3:18am

I want to create a generic "slide" sub class of DataObject that can be extended for use on different page types. E.g. some page types' slides might/might not want a title field.

I want to avoid duplicating and renaming the code of a Slide dataobject and extend the base on instead as per best OO practice. However when I try and do this, I just get SilverStripe errors. I've put my base, extended and page classes below. Does anyone have any insight into this?

Much appreciated.

The base slide class

<?php
class Slide extends DataObject {
    /**
     * 
     * DB fields
     * @var unknown_type
     */
    static $db = array (
        'Title' => 'Varchar(255)',
        'Text' => 'Text',
    	'Link' => 'Text'
    );
     
    /**
     * 
     * Relations
     * @var array
     */
    static $has_one = array (
        'Image' => 'Image'
    );
    
    /**
	 * 
	 * Fields to show in the DOM table
	 * @var array
	 */
    static $summary_fields = array(
        'Thumb' => 'Image',  
        'Title' => 'Title',
    	'Text' => 'Text'
    );
     
    /**
     * (non-PHPdoc)
     * @see httpdocs/sapphire/core/model/DataObject::getCMSFields()
     */
    public function getCMSFields()
    {
        return new FieldSet(
            new TextField('Title'),
            new TextField('Text'),
            new TextField('Link'),
            new ImageField('Image', 'Image', null, null, null, 'slides')
        );
    }
 
    /**
     * 
     * Generate our thumbnail for the DOM
     */
    public function getThumb()
    {
        if($this->ImageID)
            return $this->Image()->CMSThumbnail();
        else   
            return '(No Image)';
    }

}

The PageSlide sub class

<?php
class PageSlide extends Slide {
  
    static $has_one = array (
    	'ProductPage' => 'ProductPage',
    	'Image' => 'Image'
    );
}

Use on a page

<?php
class ProductPage extends ArticlePage {
	public static $db = array(
		'ShowOnProductHolder' => "Enum('Yes,No')",
		'Introduction' => 'Text'
	);

	public static $has_one = array(
		'PageSildes' => 'PageSlide'
	);
	
	public function getCMSFields() {
		$fields = parent::getCMSFields();
		$fields->addFieldToTab('Root.Content.Main', new TextField('Introduction', "Introduction"), 'Content');
		$manager = new DataObjectManager(
            $this,
            'PageSlides',
            'PageSlide'
        );
        $fields->addFieldToTab("Root.Content.Slideshow", $manager);
        // Add the dispaly on productholder page drop down
        $fields->addFieldToTab('Root.Content.Main', new DropdownField('ShowOnProductHolder', 'Show product on Products and applications page?', $this->dbObject('ShowOnProductHolder')->enumValues()));
        return $fields;
	}
}

Avatar
ContentBloom

Community Member, 5 Posts

1 November 2011 at 3:21am

Edited: 01/11/2011 3:22am

A bit more info, enabling development mode, I get the following error...

Line 324 in /mnt/hgfs/www/epse/httpdocs/sapphire/forms/TableListField.php
Source

315 				}
316 			});
317 JS
318 		);}
319 		return $this->renderWith($this->template);
320 	}
321 	
322 	function Headings() {
323 		$headings = array();
324 		foreach($this->fieldList as $fieldName => $fieldTitle) {
325 			$isSorted = (isset($_REQUEST['ctf'][$this->Name()]['sort']) && $fieldName == $_REQUEST['ctf'][$this->Name()]['sort']);
326 			// we can't allow sorting with partial summaries (groupByField)
327 			$isSortable = ($this->form && $this->isFieldSortable($fieldName) && !$this->groupByField);
328 
329 			// sorting links (only if we have a form to refresh with)
330 			if($this->form) {

Trace

    TableListField->Headings()
    Line 288 of DataObjectManager.php
    DataObjectManager->HasSearch()
    Line 369 of ViewableData.php
    ViewableData->obj(HasSearch,,,1,)
    Line 411 of ViewableData.php
    ViewableData->cachedCall(HasSearch,)
    Line 424 of ViewableData.php
    ViewableData->hasValue(HasSearch)
    Line 257 of .cache.dataobject_manager.templates.FileDataObjectManager.ss
    include(/tmp/silverstripe-cache-mnt-hgfs-www-epse-httpdocs/.cache.dataobject_manager.templates.FileDataObjectManager.ss)
    Line 420 of SSViewer.php
    SSViewer->process(FileDataObjectManager)
    Line 342 of ViewableData.php
    ViewableData->renderWith(FileDataObjectManager)
    Line 275 of ComplexTableField.php
    ComplexTableField->FieldHolder()
    Line 282 of DataObjectManager.php
    DataObjectManager->FieldHolder()
    Line 369 of ViewableData.php
    ViewableData->obj(FieldHolder,,,1)
    Line 446 of ViewableData.php
    ViewableData->XML_val(FieldHolder,,1)
    Line 73 of .cache.sapphire.templates.TabSetFieldHolder.ss
    include(/tmp/silverstripe-cache-mnt-hgfs-www-epse-httpdocs/.cache.sapphire.templates.TabSetFieldHolder.ss)
    Line 420 of SSViewer.php
    SSViewer->process(TabSet)
    Line 342 of ViewableData.php
    ViewableData->renderWith(TabSetFieldHolder)
    Line 80 of TabSet.php
    TabSet->FieldHolder()
    Line 369 of ViewableData.php
    ViewableData->obj(FieldHolder,,,1)
    Line 446 of ViewableData.php
    ViewableData->XML_val(FieldHolder,,1)
    Line 58 of .cache.sapphire.templates.TabSetFieldHolder.ss
    include(/tmp/silverstripe-cache-mnt-hgfs-www-epse-httpdocs/.cache.sapphire.templates.TabSetFieldHolder.ss)
    Line 420 of SSViewer.php
    SSViewer->process(TabSet)
    Line 342 of ViewableData.php
    ViewableData->renderWith(TabSetFieldHolder)
    Line 80 of TabSet.php
    TabSet->FieldHolder()
    Line 369 of ViewableData.php
    ViewableData->obj(FieldHolder,,,1)
    Line 446 of ViewableData.php
    ViewableData->XML_val(FieldHolder,,1)
    Line 77 of .cache.sapphire.templates.Includes.Form.ss
    include(/tmp/silverstripe-cache-mnt-hgfs-www-epse-httpdocs/.cache.sapphire.templates.Includes.Form.ss)
    Line 420 of SSViewer.php
    SSViewer->process(Form)
    Line 342 of ViewableData.php
    ViewableData->renderWith(Array)
    Line 1108 of Form.php
    Form->forTemplate()
    Line 447 of ViewableData.php
    ViewableData->XML_val(EditForm,,1)
    Line 13 of .cache.cms.templates.Includes.CMSMain_right.ss
    include(/tmp/silverstripe-cache-mnt-hgfs-www-epse-httpdocs/.cache.cms.templates.Includes.CMSMain_right.ss)
    Line 420 of SSViewer.php
    SSViewer->process(CMSMain)
    Line 342 of ViewableData.php
    ViewableData->renderWith(Array)
    Line 492 of LeftAndMain.php
    LeftAndMain->Right()
    Line 369 of ViewableData.php
    ViewableData->obj(Right,,,1)
    Line 446 of ViewableData.php
    ViewableData->XML_val(Right,,1)
    Line 71 of .cache.cms.templates.LeftAndMain.ss
    include(/tmp/silverstripe-cache-mnt-hgfs-www-epse-httpdocs/.cache.cms.templates.LeftAndMain.ss)
    Line 420 of SSViewer.php
    SSViewer->process(CMSMain)
    Line 202 of Controller.php
    Controller->handleAction(SS_HTTPRequest)
    Line 143 of RequestHandler.php
    RequestHandler->handleRequest(SS_HTTPRequest)
    Line 147 of Controller.php
    Controller->handleRequest(SS_HTTPRequest)
    Line 282 of Director.php
    Director::handleRequest(SS_HTTPRequest,Session)
    Line 125 of Director.php
    Director::direct(/admin)
    Line 127 of main.php