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

Relation between two DataObjects


Go to End


5 Posts   1987 Views

Avatar
TT

Community Member, 6 Posts

10 January 2009 at 1:09am

Edited: 10/01/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);
}
}
}

Avatar
UncleCheese

Forum Moderator, 4102 Posts

10 January 2009 at 4:23am

What does $game->GameIdent(); return?

Avatar
TT

Community Member, 6 Posts

10 January 2009 at 8:29am

Edited: 10/01/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.

Avatar
UncleCheese

Forum Moderator, 4102 Posts

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.

Avatar
TT

Community Member, 6 Posts

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?