Jump to:

3372 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Trouble determining best model (UPDATED)

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

Page: 1
Go to End
Author Topic: 971 Views
  • jondbaker
    Avatar
    Community Member
    19 Posts

    Trouble determining best model (UPDATED) Link to this post

    The idea is that when adding a new Link the user will be able to select a LinkCategory from a DropdownField. This is to allow me to structure the template in a way that I can control each category and then each specific category's links.
    Error Message:

    [User Error] Uncaught Exception: Object->__call(): the method 'fortemplate' does not exist on 'LinkCategory'
    GET /public_html/jonathandbaker/admin

    Line 576 in D:\xampp\xampp\htdocs\public_html\jonathandbaker\sapphire\core\Object.php

    LinkHolder.php

    <?php
    class LinkHolder extends Page {
    static $singular_name = 'LinkHolder';
    static $plural_name = 'LinkHolders';
    static $db = array(
    'Headline' => 'Text',
    );
    static $has_many = array(
    'LinkCategories' => 'LinkCategory'
    );
    function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->removeFieldFromTab("Root.Content.Main", "Content");
    $fields->addFieldToTab('Root.Content.Main', new TextField('Headline'));
    $linksTable = new ComplexTableField(
    $controller = $this,
    $name = "Links",
    $sourceClass = "Link",
    $fieldList = array(
    'LinkURI' => 'Link URI',
    'LinkDisplayText' => 'Link Display Text',
    'LinkCategory' => 'Link Category'
    )
    );
    $fields->addFieldToTab('Root.Content.Links', $linksTable);
    return $fields;
    }
    }
    class LinkHolder_Controller extends Page_Controller {}
    ?>

    LinkCategory.php

    <?php
    class LinkCategory extends DataObject {
    static $db = array(
    'LinkCategory' => 'Varchar(100)'
    );
    static $singular_name = 'LinkCategory';
    static $plural_name = 'LinkCategories';
    static $has_one = array(
    'LinkHolder' => 'LinkHolder'
    );
    static $has_many = array(
    'Links' => 'Link'
    );
    }
    ?>

    Link.php

    <?php
    class Link extends LinkCategory {
    static $db = array(
    'LinkURI' => 'Text',
    'LinkDisplayText' => 'Text',
    );
    static $singular_name = 'Link';
    static $plural_name = 'Links';
    static $has_one = array(
    'LinkCategory' => 'LinkCategory'
    );
    public function getCMSFields(){
    $categories = array(
    'Mis Amigos' => 'Mis Amigos',
    'jQuery' => 'jQuery',
    'Blogs' => 'Blogs',
    'Open-Source' => 'Open-Source',
    'Design Inspiration' => 'Design Inspiration',
    'Web Resources' => 'Web Resources'
    );
    $fields = new FieldSet(
    new TextField('LinkURI', 'Link URI'),
    new TextField('LinkDisplayText', 'Link Display Text'),
    new DropdownField('LinkCategory','LinkCategory', $categories)
    );
    return $fields;
    }
    }
    ?>

  • wee-man
    Avatar
    Community Member
    21 Posts

    Re: Trouble determining best model (UPDATED) Link to this post

    Hi,

    i think the problem is in the Link Class.

    The Link model has_one LinkCategory, which is ja DataObject.
    In the form you use a Dropdown to give a number of possible LinkCategories.
    In my opinion there is the point: DataObject <-> String.

    To solve the Problem you can rename the DropdownField and work with the onBeforeWrite() in Link.
    In onBeforeWrite() you can assign a DataObject to the Link depending on the DropdownField value.

    Greetings
    Michael

    971 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.