Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

Automatic $has_one relationship


Go to End


3 Posts   869 Views

Avatar
tv

Community Member, 44 Posts

1 July 2011 at 1:22pm

Edited: 02/07/2011 12:02am

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!

Avatar
martimiz

Forum Moderator, 1391 Posts

3 July 2011 at 1:18am

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

Avatar
tv

Community Member, 44 Posts

3 July 2011 at 6:35am

Thanks alot for clearing that up for me!