Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » One set of categories applying to 2 sets data

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

Page: 1
Go to End
Author Topic: 801 Views
  • phawley
    Avatar
    Community Member
    21 Posts

    One set of categories applying to 2 sets data Link to this post

    I'm wondering how exactly to make the many_many example apply to a situation in which I need *one* common set of categories that can be applied to 2 distinct sets of info (Articles and Videos).

    I've tried just allowing the belongs_many_many to point to Articles (and crossing fingers) but when a new Category entry is created from Articles, the record is created with a Title of NULL, and same result from Videos, but 2 records are created.

    Thanks very much,
    Patrick

  • phawley
    Avatar
    Community Member
    21 Posts

    Re: One set of categories applying to 2 sets data Link to this post

    Okay...solved it myself with some better forum searches and rewriting it slightly differently:

    Article.php (Video.php is same but with Video replacing Article obviously)

    <?php

    class Article extends Page {

    static $db = array(
    'Author' => 'Text'
    );

    static $many_many = array(
    'Topics' => 'Topic'
    );

    static $allowed_children = 'none';

    function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->addFieldToTab('Root.Content.Main', new TextField('Author'), 'Content');
    $topicsTablefield = new ManyManyComplexTableField(
    $this,
    'Topics',
    'Topic',
    array(
    'TopicName' => 'Topics'
    ),
    'getCMSFields_forPopup'
    );
    $topicsTablefield->setAddTitle( 'A Topic' );

    $fields->addFieldToTab( 'Root.Content.Topics', $topicsTablefield );

    return $fields;
    }

    }

    class Article_Controller extends Page_Controller {

    }

    ?>

    Topic.php

    <?php

    class Topic extends DataObject {

    static $db = array(
    'TopicName' => 'Text'
    );

    static $belongs_many_many = array(
    'Articles' => 'Article'
    );

    function getCMSFields_forPopup() {
    $fields = new FieldSet();
    $fields->push( new TextField( 'TopicName', 'Topic' ) );
    return $fields;
    }

    }

    Hope providing this code helps someone else,
    Patrick

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