Now I’m extending the HomePage.php Page type.
- We add db arrays:
<?php class HomePage extends Page { static $db = array( ); static $has_one = array( ); } class HomePage_Controller extends Page_Controller { } ?>
- We can use the database arrays to add extra fields to the database. Let’s one image, One Description:
<?php class HomePage extends Page { static $db = array( 'Description' => 'Text' ); static $has_one = array( 'Photo' => 'Image' ); } class HomePage_Controller extends Page_Controller { } ?>
- We now have the extra fields in the database, but still no way of changing them. To add these fields to the CMS we have to override the getCMSFields() method, which is called by the CMS when it creates the form to edit a page. Add the method to the HomePage class like so:
<?php class HomePage extends Page { static $db = array( 'Description' => 'Text' ); static $has_one = array( 'Photo' => 'Image' ); function getCMSFields() { $fields = parent::getCMSFields(); $fields->addFieldToTab('Root.Content.Main', new ImageField('Photo'), 'Content'); $fields->addFieldToTab('Root.Content.Main', new TextField('Description'), 'Content'); return $fields; } } class HomePage_Controller extends Page_Controller { } ?>
- After you have added the arrays and CMS fields for your Pagetype don’t forget to build the database for it using /dev/build/:
yoursite.com/dev/build/?flush=1
Looking for quality SilverStripe Web Hosting? Look no further than Arvixe Web Hosting!