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.

Data Model Questions /

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

Trouble determining best model (UPDATED)


Go to End
Reply


2 Posts   1172 Views

Avatar
jondbaker

Community Member, 19 Posts

19 December 2009 at 2:19pm

Edited: 20/12/2009 11:26am

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;
   }
}
?>

Avatar
wee-man

Community Member, 21 Posts

21 December 2009 at 9:16am

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