Jump to:

23477 Posts in 18941 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Relation between two DataObjects

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

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

Page: 1
Go to End
Author Topic: 1041 Views
  • TT
    Avatar
    Community Member
    6 Posts

    Relation between two DataObjects Link to this post

    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);
          }
       }
    }

  • UncleCheese
    Avatar
    4085 Posts

    Re: Relation between two DataObjects Link to this post

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

  • TT
    Avatar
    Community Member
    6 Posts

    Re: Relation between two DataObjects Link to this post

    It returns just information about a single game put together using the database fields for that GameObject - some kind of special title.

  • UncleCheese
    Avatar
    4085 Posts

    Re: Relation between two DataObjects Link to this post

    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.

  • TT
    Avatar
    Community Member
    6 Posts

    Re: Relation between two DataObjects Link to this post

    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?

    1041 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.