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

One set of categories applying to 2 sets data


Go to End
Reply


2 Posts   930 Views

Avatar
phawley

Community Member, 21 Posts

4 July 2010 at 2:09pm

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

Avatar
phawley

Community Member, 21 Posts

5 July 2010 at 3:48pm

Edited: 05/07/2010 3:48pm

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