Jump to:

23556 Posts in 19383 Topics by 2892 members

General Questions

SilverStripe Forums » General Questions » Automatic $has_one relationship

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 534 Views
  • tv
    Avatar
    Community Member
    44 Posts

    Automatic $has_one relationship Link to this post

    Hi

    I have a class NewRelease that extends DataObject with a $has_one to a Page:

    static $db = array('Artist' => 'Varchar(64)');

    static $has_one = array(
    'Page' => 'NewsPage',
    );

    And a Page with a $has_many to NewRelease:

    static $has_many = array(
    'NewReleases'      => 'NewRelease'
    );

    I also have $fields->removeByName('Page') in the getCMSFields function of NewRelease. I don't want the CMS user to have to select 'News' from a droop down field to create the relationship.

    Whenever I create and save a new 'NewRelease' and then do

    <% control NewReleases %>
    $Artist
    <% end_control %>

    in my NewPage.ss template, I am not getting any results

    I am extending ModelAdmin to handle the NewRelease DataObject organization and therefore don't have a getCMSFields function for NewRelease represented in NewsPage.php. Could this be causing the problem?

    My second question is, as a work around, I have created a public function Releases() { return DataObject::get('NewRelease', null, null, null, 6); } to pull the NewReleases into NewsPage.ss:

    <% control Releases %>
    $Artist
    <% end_control %>

    Is this a bad solution since I still notice there is no NewsPageID being created on NewRelease and therefore no relationship being formed in the database.

    Thanks in advance. This has been driving me crazy!

  • martimiz
    Avatar
    Forum Moderator
    1091 Posts

    Re: Automatic $has_one relationship Link to this post

    The point is: do you need/want to have new releases belonging to a specific news page. Are you going to have multiple news pages that have different sets of new releases belonging to them? If so - you need the pageID to be there always!

    If you're only ever going to have one news page for all new releases you don't need the has_one/has_many relation at all! just do the DataObject::get for all new releases on the newspage.

    One way or the other it's never good practice to define a relation and then not creating it - half of the time...

  • tv
    Avatar
    Community Member
    44 Posts

    Re: Automatic $has_one relationship Link to this post

    Thanks alot for clearing that up for me!

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