Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » $has_many -> $has_many -> $belongs_many_many

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

Page: 1
Go to End
Author Topic: 2124 Views
  • Martijn
    Avatar
    Community Member
    271 Posts

    $has_many -> $has_many -> $belongs_many_many Link to this post

    I have one PageType CompanyPage which has many Companies and many Regions.
    Each Company can be active in many Regions.

    When I create a company, I want to assign several regions to it.
    Like this, but in the Company editscreen:
    http://doc.silverstripe.com/lib/exe/fetch.php?w=&h=&cache=cache&media=tutorial:gsoc-mentor-student-selection.png

    So I need multiple CTF's but can't figure it out wich relations I need and which CTF's I need to manage them.
    I can add Companies and Regions to the CompanyPage, but I can't assign Regions to a Company when editting a Company.

    This are the realtions that I now have

    class CompanyPage extends Page {
       
       public static $db = array(
       );
       
       static $has_many = array (
          'Companies' => 'Company',
          'Regions' => 'Region'

       );

    }

    class Company extends DataObject {
    static $db = array (
          'Name' => 'Varchar'
    );
    static $has_one = array (
          'Logo' => 'Image',
          'CompanyPage' => 'CompanyPage'

       );
    static $many_many = array(
    'Regions' => 'Region'
    );
    }

    class Region extends DataObject {

       static $db = array (
          'Name' => 'Varchar'
       );

       static $has_one = array(
             'CompanyPage' => 'CompanyPage'
    );
       
        static $belongs_many_many = array(
           'Companies' => 'Company'
        );

    }


    On the CompanyPage I have 2 HasManyComplexTableFields for showing the Companies and Regions in a seperate tab. This works as expected.

    I can add Regions. No problem.

    Now when I add a ManyManyComplexTableField to the Company class I get this error:

    Fatal error: Call to a member function Regions() on a non-object in D:\xampp\xampp\htdocs\silver_snippets\sapphire\forms\HasManyComplexTableField.php on line 91

    When I add a HasManyComplexTableField I get:
    Can't find a has_one relationship from 'Region' to 'Company'

    So when I add a $has_one to the Region class, I can choose a Company on creating a new Regio. But it doesn't save the Company to the Region..

    Can someone point me to the right direction to relate this 3 classes and how to administer them?

  • Bambii7
    Avatar
    Community Member
    254 Posts

    Re: $has_many -> $has_many -> $belongs_many_many Link to this post

    I'm no guru, just fumbling my way through silverstripe. I had this issue the other day with a manymanycomplextablefield. I found by removing the relationship for the blongs_many_many worked and saved. I was getting the exact same thing. Try removing the companies relationship from region.

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