Reputation: 486
What is the best way to manage a 1:1 relation in Silverstripe with the following example. It's easy to add the $db fields to the page, but if I want to maintain it as a separate Dataobject, whats the best way? (Lot of info on 1:many, but not 1:1) eg.
class CarSpecs extends Dataobject {
private static $db = array(
'Make' => 'Text',
'Model' => 'Text',
'ModelDescription' => 'Text',
'NumberOfSeats' => 'VarChar(20)',
'Price' => 'Currency'
}
class Car extends Page {
private static $has_one = array('CarSpecs' => 'CarSpecs');
}
Silverstripe automatically adds a page ID to the database table in Car to find CarSpecs, but what about in Carspecs? How to I add and edit the related fields in the CMS.
Upvotes: 1
Views: 854
Reputation: 146
You're probably looking for belongs_to
Defines a 1-to-1 relationship with another object, which declares the other end of the relationship with a
corresponding $has_one. A single database column named <relationship-name>ID
will be created in the object with the
$has_one
, but the $belongs_to by itself will not create a database field. This field will hold the ID of the object
declaring the $belongs_to
.
Similarly with $has_many, dot notation can be used to explicitly specify the $has_one
which refers to this relation.
This is not mandatory unless the relationship would be otherwise ambiguous.
<?php
class Team extends DataObject {
private static $has_one = array(
'Coach' => 'Coach'
);
}
class Coach extends DataObject {
private static $belongs_to = array(
'Team' => 'Team.Coach'
);
}
Based on your comment and a re-read of the question I'm guessing your wanting a way to edit CarSpecs separately from the CarPages, in which case you're looking for the ModelAdmin with something like:
<?php
class CarSpecsAdmin extends ModelAdmin {
private static $managed_models = array(
'CarSpecs'
);
private static $url_segment = 'CarSpecs';
private static $menu_title = 'Car Specs Admin';
}
Upvotes: 2