21282 Posts in 5730 Topics by 2601 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 390 Views |
-
Automatic $has_one relationship

1 July 2011 at 1:22pm Last edited: 2 July 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!
-
Re: Automatic $has_one relationship

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...
| 390 Views | ||
|
Page:
1
|
Go to Top |

