Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » New Feature: Intelligent Constructor

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1577 Views
  • UncleCheese
    Avatar
    4085 Posts

    New Feature: Intelligent Constructor Link to this post

    Many times when using a DOM on your page, you're forced to feed the object a lot of information that is repetitive and redundant. The DataObjectManager and all of its children will now make assumptions about the name, source class, headings, and popup fields. In the following example, I have a very simple DataObject with a very simple relation to its page:

    Product.php

    class Product extends DataObject
    {
    static $db = array (
    'Title' => 'Varchar(50)',
    'Description' => 'Text',
    'Price' => 'Decimal',
    'OutOfStock' => 'Boolean'

    );

    static $has_one = array (
    'StorePage' => 'StorePage',
    'ProductImage' => 'Image'
    );

    function getCMSFields()
    {
    return new FieldSet(
    new TextField('Title'),
    new TextareaField('Description'),
    new NumericField('Price'),
    new CheckboxField('OutOfStock','This product is out of stock'),
    new ImageField('ProductImage')
    );
    }
    }

    StorePage.php

    class StorePage extends Page
    {
       static $has_many = array (
        'Products' => 'Product'
       );
       
       public function getCMSFields()
       {
          $fields = parent::getCMSFields();
    $fields->addFieldToTab("Root.Content.Products", new DataObjectManager($this));      
          return $fields;
       }
    }

    class StorePage_Controller extends Page_Controller
    {
    }

    Notice that we have greatly reduced the amount of data we need to feed the DataObjectManager function. Even though we have only passed the controller ($this), it still produces the following result:

    Without the intelligent constructor, the code would look like this:

    new DataObjectManager(
    $this,
    'Products',
    'Product',
    array(
    'Title' => 'Title',
    'Description' => 'Description',
    'Price' => 'Price',
    'OutOfStock' => 'Out Of Stock'
    ),
    'getCMSFields'
    );

    If we want to customize the table headings, we can define in the DataObject:

    static $summary_fields = array (
    'Title' => 'Title',
    'Description' => 'Description'
    );

    This produces the following:

    How it works

    It's not rocket science.

    - If the name AND source class are left undefined, it will take the first has_many relationship off your controller. In this case, my controller $has_many Products, so it uses that. Right now, relation DOMs will not be able to use intelligent constructors.

    - If the headings are left undefined, it loops through your $db array, and formats your fields with a friendly string. (OutOfStock becomes Out Of Stock).

    - If the popup fields are left undefined, it uses getCMSFields(). This has always been true with DOM and CTF alike. Very handy.

    That's it! For more complex pages, you'll obviously still have to use the full constructor, but if you're like me, you get into a lot of situations where feeding the DOM a bunch of data it should be able to figure out on its own is a bit tedious.

    Enjoy!

  • banal
    Avatar
    Community Member
    901 Posts

    Re: New Feature: Intelligent Constructor Link to this post

    Nice feature UncleCheese. Reduces setup time and avoids common mistakes.
    When a DataObject has been set to sortable, the "Sort Order" Column also shows up in the automatically generated table headers. I would probably prevent that from happening.. apart from this, another sweet new feature! Thanks.

  • UncleCheese
    Avatar
    4085 Posts

    Re: New Feature: Intelligent Constructor Link to this post

    Great catch. Thanks, Banal.

    1577 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.