21493 Posts in 5784 Topics by 2622 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 931 Views |
-
Relation between two DataObjects

10 January 2009 at 1:09am Last edited: 10 January 2009 2:38am
Hello,
I have problem using relation between two DataObjects: ArticleObject and GameObject. Both objects are used within Holders (ArticlePage and SchedulePage) as has_many relations using the has_one in the Objects for the reverse relation. When editing a GameObject it should be possible to select a given Article for this Game. Using a DropDownField updates the GameObject table (the ID of the Article is written to the correct column) but the ArticleObject is never updated. The ID of the Game linking to the Article should also be stored there in order to find the way back. The other way the same when trying to establish the relation by editing the Article and linking to the Game.I think I have done everything described in the tutorials. But why is the DropDownField not updating the related Object and how can I achieve this?
Thanks.
Here are the classes:
class GameObject extends DataObject {
static $has_one = array(
'Schedule' => 'SchedulePage',
'Article' => 'ArticleObject'
);public function getCMSFields_forPopup() {
$fields = new FieldSet();
...
$articles = DataObject::get('ArticleObject', 'GameID = 0 OR GameID = ' . $this->ID, 'Created DESC', '', '10');
if (!$articles) {
$articleMap = null;
} else {
$articleMap = $articles->toDropDownMap('ID', 'Title');
}
$articleDropDown = new DropdownField('ArticleID', 'Bericht', $articleMap, $this, 'null', true);
$fields->push($articleDropDown);}
}class ArticleObject extends DataObject {
static $has_one = array(
'ArticleContainer' => 'ArticlePage',
'Game' => 'GameObject'
);public function getCMSFields_forPopup() {
$fields = new FieldSet();
...
$games = DataObject::get('GameObject', 'ArticleID = 0 OR ArticleID = ' . $this->ID, 'Created DESC', '', '10');
if ($games) {
$gameMap = array();
foreach ($games as $game) {
$gameMap[$game->ID] = $game->GameIdent();
}
$gameDropDown = new DropdownField('GameID', 'Bericht', $gameMap, $this, 'null', true);
$fields->push($gameDropDown);
}
}
} -
Re: Relation between two DataObjects

10 January 2009 at 4:23am
What does $game->GameIdent(); return?
-
Re: Relation between two DataObjects

10 January 2009 at 8:29am Last edited: 10 January 2009 8:35am
It returns just information about a single game put together using the database fields for that GameObject - some kind of special title.
-
Re: Relation between two DataObjects

10 January 2009 at 10:17am
Okay, I think I understand this. You've really got a one-to-one set up with Article->Game. I guess I'm having a hard time understanding what the purpose of that is. Can you give me an example where the one-to-one is needed?
Sorry if my questions seem uninformed. I'm just trying to get a better idea of what you want.
-
Re: Relation between two DataObjects

10 January 2009 at 10:29am
That's correct. There are several relations between 4 types of objects. The one I have trouble with is the 1-to-1 relation between those two objects described above.
The problem is that whenever I try to link one object to the other the CMS only updates one table instead of both.
An Example:
When I edit an ArticleObject I can insert the ID of the GameObject into the GameID field of the ArticleObject table using the DropDownField list. But when saving the ArticleObject the GameObject table should be updated, too, because there is a field called 'ArticleID'. This will allow me to find the ArticleObject when I use the GameObject (that's how I understand the 1-to-1 relation).Sure, I could search for the ArticleObject using a filter on the GameID field but I want the possibility to establish the relation from both ends when editing either the ArticleObject or the GameObject. In both cases the respective other Object needs to get an update, too.
Is such a behavior possible in SilverStripe?
| 931 Views | ||
|
Page:
1
|
Go to Top |
