Jump to:

3461 Posts in 1065 Topics by 740 members

Data Model Questions

SilverStripe Forums » Data Model Questions » belongs_many_many to many_many accessor

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

Page: 1
Go to End
Author Topic: 3973 Views
  • jahbini
    Avatar
    Community Member
    13 Posts

    belongs_many_many to many_many accessor Link to this post

    I need to access all words belonging to sentences, and ALSO list all sentences that contain a given word. (this is not the real example, but it's as close as I can come and explain easily). A has many_many and belongs_many_many seems the right choice, but the accessor does not work from the 'belongs_many_many side. It seems like it should be easy, but I must be missing something. Any help?

    class Sentence extends DataObject {
    ....
    static $many_many = array( 'Words' => 'Word');

    function getMyWords() {
    return $this->getManyManyComponents('Words'); //This works great!
    }
    ....
    }

    class Word extends DataObject {
    ....
    static $belongs_many= array('Sentences' => 'Sentence');
    ...
    function getMySentances() {
    return $this -> getManyManyComponents('Sentences'); // This does not work. What to do? Can it be done?
    }
    ...
    }

  • UncleCheese
    Avatar
    4085 Posts

    Re: belongs_many_many to many_many accessor Link to this post

    Have you tried just:

    return $this->Sentences() ?

  • jahbini
    Avatar
    Community Member
    13 Posts

    Re: belongs_many_many to many_many accessor Link to this post

    No, $this -> Sententences() was my very first stab at this, and it was not working, but I did a bit of spelunking into the caverns of Sapphire, and found another (undocumented? underdocumented?) static array called

    belongs_many_many

    I added that to the class 'Word' and it seems to work (cool!), but are 'belongs_many' and 'belongs_many_many' both required?

    class Sentence extends DataObject {
    ....
    static $many_many = array( 'Words' => 'Word');

    function getMyWords() {
    return $this->getManyManyComponents('Words'); //This works great!
    }
    ....
    }

    class Word extends DataObject {
    ....
    static $belongs_many= array('Sentences' => 'Sentence');
    static $belongs_many_many = array('Sentences' => 'Sentence');
    ...
    function getMySentances() {
    return $this -> getManyManyComponents('Sentences'); // This NOW does work.
    }
    ...
    }

  • UncleCheese
    Avatar
    4085 Posts

    Re: belongs_many_many to many_many accessor Link to this post

    Weird.. I've never heard of belongs_many. Only belongs_many_many. You definitely want belongs_many_many in your words object. Not sure what belongs_many is...

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: belongs_many_many to many_many accessor Link to this post

    Uhm, there is no such thing as "belongs_many" - and I'm fairly certain that $myWord->Sentences() should work out of the box, without you having to overload getMySentances() (please note the spelling mistake from your code). We're using belongs_many_many accessors in our DataObjectTest unit tests, and they don't give us any trouble.

  • jahbini
    Avatar
    Community Member
    13 Posts

    Re: belongs_many_many to many_many accessor Link to this post

    Yes! Thanks for that. I'll just get rid of the belongs_many from my code (and my brain).

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