Jump to:

7935 Posts in 1536 Topics by 943 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » HOWTO: HasMany and ManyMany new items default to checked

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: 1823 Views
  • UncleCheese
    Avatar
    4085 Posts

    Re: HOWTO: HasMany and ManyMany new items default to checked Link to this post

    I posted this solution a while ago. You shouldn't need to update the javascript or anything.

    This is what I do:

       public function onAfterWrite()
       {
          parent::onAfterWrite();
          if(isset($_REQUEST['ctf'])) {
             // new record?
             if(!isset($_REQUEST['ctf']['childID']))
                DB::query("INSERT INTO `[YOUR_JOIN_TABLE]` SET [PARENT_TABLE_ID] = ". $_REQUEST['ctf']['sourceID'] . ", [CHILD_TABLE_ID] = $this->ID");
       

          }
       }

  • UncleCheese
    Avatar
    4085 Posts

    Re: HOWTO: HasMany and ManyMany new items default to checked Link to this post

    Oh, right. Yeah, I forgot about that hack. Cool, thanks for the heads up.

  • Garrett
    Avatar
    Community Member
    245 Posts

    Re: HOWTO: HasMany and ManyMany new items default to checked Link to this post

    Hey gents--

    I'm trying to do the opposite -- I have a Category DataObject which has a $belongs_many_many relationship to a NewsPage, which has the corresponding $many_many there. As a result I have the relation table NewsPage_Categories.

    What I'd like to do is to automatically associate a given Category with a newly created page so that there is always at least one Category selected. Seems like it would just be a simple INSERT INTO in an OnAfterWrite() function but alas, no such luck. It's not happening for me. here's my very simple code (in the NewsPage class):

    function onAfterWrite() {
          
    parent::onAfterWrite();
          
          if(isset($_REQUEST['ctf'])) {
             DB::query("INSERT INTO `NewsPage_Categories` SET `NewsPageID` = '".$_REQUEST['ctf']['sourceID']."', `CategoryID` = '1'");
          }
       }

    What's wrong with this picture? I surmised from the code posted above that the $_REQUEST['ctf']['sourceID'] is the ID of the newly created Page. But I am not getting an error; it's just that the insert doesn't occur. onAfterWrite(), however, is firing.

    Thanks in advance,
    Garrett

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