Jump to:

7937 Posts in 1537 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Automatically linking records (many_many)

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: 939 Views
  • Mad_Clog
    Avatar
    Community Member
    78 Posts

    Automatically linking records (many_many) Link to this post

    Submission: http://pastie.org/private/0hrmsxz5p855dyacrsnnna
    Author: http://pastie.org/private/hbwbxkknehakhw7ooh9i2q

    I open up a submission, hit the Authors tab and add a record
    Now when i save this record it gets added to the total list of Authors, however it's not linked with the currently active Submission from which i was editing.

    Is there an option i'm missing or should i add an onBeforeWrite method in the Author class?
    If so is there an easy way to link an Author to the Sumbission or should i manually write an INSERT query and execute this using the DB class?

  • UncleCheese
    Avatar
    4085 Posts

    Re: Automatically linking records (many_many) Link to this post

    Did you check it off in the MMDOM?

  • Mad_Clog
    Avatar
    Community Member
    78 Posts

    Re: Automatically linking records (many_many) Link to this post

    What I'm looking for is adding the author record and linking it with the submission at the same time.
    That way the user doesn't have to manually link the Author to the Sumbission once the Author has been added.

    When that's done I can use MMDOM->set_only_related which enforces only linked records to be shown.
    The reason behind this is that list of Authors can be come quite huge over time, so narrowing them down to only related records would be a big step forward in usability.

  • UncleCheese
    Avatar
    4085 Posts

    Re: Automatically linking records (many_many) Link to this post

    I did implement that on a recent project. Using onBeforeWrite().. I'll see if I can find a code snippet for you.

  • UncleCheese
    Avatar
    4085 Posts

    Re: Automatically linking records (many_many) Link to this post

    Here you go:

       public function onAfterWrite()
       {
          parent::onAfterWrite();
          if(isset($_POST['ctf'])) {
             if(!isset($_POST['ctf']['childID']))
                DB::query("INSERT INTO `YOUR_LINKING_TABLE` SET YourPageTypeID = ". $_POST['ctf']['sourceID'] . ", YourDataObjectID = $this->ID");
    }

  • Mad_Clog
    Avatar
    Community Member
    78 Posts

    Re: Automatically linking records (many_many) Link to this post

    Aye that's just what I was thinking about implementing, only using onBeforeWrite so you could check if it's a new record or an existing one.
    Was hoping there would be a more elegant solution to this, but this will do the trick.
    I'll have a go at this tomorrow.

    Cheers!

  • UncleCheese
    Avatar
    4085 Posts

    Re: Automatically linking records (many_many) Link to this post

    Problem is, at onBeforeWrite(), you don't have an ID, so that's why there's the clever way of evaluating if the record is new.

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