Jump to:

3429 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » In the case of a 'has-many' relation, do I have to declare the "has-one" relation and what if I don't ?

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

Page: 1
Go to End
Author Topic: 911 Views
  • lise
    Avatar
    Community Member
    47 Posts

    In the case of a 'has-many' relation, do I have to declare the "has-one" relation and what if I don't ? Link to this post

    It would be a bit complex to explain why I want to do that but basically I am wondering if I can define something like :

    class customers extends page
    {

    static $has_many = array(
             'country' => 'country'
       );
    }

    and

    class country extends DataObject
    {
    static $db = array(
    'CountryName' => 'Text',
    );
    }

    Note that I did *NOT* define the has-one relation in 'country'.

    I know that by omitting the reverse "has-one" relation I can not point to a list of customers linked to the same country but
    I know I will never need to .

    So my question is : am I breaking the data model by doing this ? What risk am I taking here?

    I tried and it works ...I just want to make sure I am not overlooking something critical.

    Any advice and comments would be appreciated.

    Thanks,
    Lise

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: In the case of a 'has-many' relation, do I have to declare the "has-one" relation and what if I don't ? Link to this post

    I'm a bit surprised that it still works - the has_one relation is really the important one in a one-many relation (it defines the other object id field in the database). Usually you can do without the has_many, but need the has_one, but in your case you should need both.

    I would recommend you set it up correctly. The risk is that you've created the database field in a previous build, but removed the definition, so it might fail if you tried to rebuild from a fresh database.

  • lise
    Avatar
    Community Member
    47 Posts

    Re: In the case of a 'has-many' relation, do I have to declare the "has-one" relation and what if I don't ? Link to this post

    Hamish,

    Thank you for your reply ...you are probably very right because I am now experiencing s problem which, I suspect, is due to
    the way I (incorrectly) define the relation. I will follow your advice.

    Thanks again

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