Jump to:

7938 Posts in 1542 Topics by 945 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Nested HasOneDOM not Writing Relation

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 475 Views
  • zenmonkey
    Avatar
    Community Member
    528 Posts

    Nested HasOneDOM not Writing Relation Link to this post

    I have a bit of complex nested HasOneDom thats not writing the Relation ID's and I've been banging my head against it and not getting anywhere

    Here's the Parent Relation

    static $has_one = array(
          'BundlePage' => 'BundlePage', //Holding Page Type 1
          'FeaturePage' => 'FeaturePage', //Holdeing Page Type 2
          'IlsItem' => 'IlsItem', //Sibling relation
          'ThisBundleQty'=>'BundleQty' // Child Relation
       );

    And Here's the code that generates the popup

       public function getCMSFields_forPopup()
       {
          //This Part just Builds the Form Based on the type of Item the Sibling it Is
          if($this->IlsItemID){
             $ilsItem = DataObject::get_by_id("IlsItem", $this->IlsItemID);
             $upcs = DataObject::get("IlsUPC", "IlsItemID = ".$this->IlsItemID);
             
             if ($ilsItem && $ilsItem->CollectionBook == "Eyelash") {
                $sizeFields = array('GenQty' => 'GenQty');
                $finalSize = "Gen";
             } else {
                if($upcs) {
                   if ($upcs->count() == 1) {
                      $sizeFields = array("OneSizeQty" => "OneSizeQty");
                      $finalSize = "OS";
                   } else {
                      $sizeArray = array();
                      foreach ($upcs as $size) {
                         array_push($sizeArray, $size->Size);
                      }
                      if(in_array("32B", $sizeArray)) {
                         $sizeFields = array(
                            '32AQty' => '32AQty',
                            '32BQty' => '32BQty',
                            '32CQty' => '32CQty',
                            '34BQty' => '34BQty',
                            '34CQty' => '34CQty',
                            '34DQty' => '34DQty',
                            '34DDQty' => '34DDQty',
                            '36BQty' => '36BQty',
                            '36CQty' => '36CQty',
                            '36DQty' => '36DQty',
                            '36DDQty' => '36DDQty',
                            '38BQty' => '38BQty',
                            '38CQty' => '38CQty',
                            '38DQty' => '38DQty',
                            '38DDQty' => '38DDQty'
                         );
                         $finalSize = "Bra";
                      } elseif(in_array("M/L", $sizeArray)) {
                         $sizeFields = array(
                            'SMQty' => 'SMQty',
                            'MLQty' => 'MLQty'
                         );
                         $finalSize = "SMML";
                      } else {
                         $sizeFields = array(
                            'SmallQty' => 'SmallQty',
                            'MediumQty' => 'MediumQty',
                            'LargeQty' => 'LargeQty'
                         );
                         $finalSize = "SML";
                      }
                      
                   }
                } else {
                   $sizeFields = array("nonefound" => "nonefound");
                   $finalSize = "All";
                }
             }
          }
             
          return new FieldSet(
             new DropdownField('IlsItemID', 'Choose an Item', $this->getBaciItems()),
             new HasOneDataObjectManager(
                $this,
                'ThisBundleQty',
                'BundleQty',
                $sizeFields,
                'getCMSFields_forPopup'
             )
             
          );
       }

    Now Here's the Child DataObject

    class BundleQty extends DataObject {
       
       static $db = array(
          'SmallQty' => 'Int',
          'MediumQty' => 'Int',
          'LargeQty' => 'Int',
          'OneSizeQty' => 'Int',
          'SMQty' => 'Int',
          'MLQty' => 'Int',
          '32AQty' => 'Int',
          '32BQty' => 'Int',
          '32CQty' => 'Int',
          '34BQty' => 'Int',
          '34CQty' => 'Int',
          '34DQty' => 'Int',
          '34DDQty' => 'Int',
          '36BQty' => 'Int',
          '36CQty' => 'Int',
          '36DQty' => 'Int',
          '36DDQty' => 'Int',
          '38BQty' => 'Int',
          '38CQty' => 'Int',
          '38DQty' => 'Int',
          '38DDQty' => 'Int',
          'GenQty' => 'Int'
       );
       
       static $has_one = array(
          'BundleItem' => 'BundleItem'
       );
       
       
       //I had to use a hacky way to get the figure out which of the fields to render in the form
       public function getCMSFields_forPopup()
       {
          if($this->BundleQtyID){
             $bundleItem = DataObject::get_by_id("BundleItem", $this->BundleQtyID);
             $ilsItem = DataObject::get_by_id("IlsItem", $bundleItem->IlsItemID);
             $upcs = DataObject::get("IlsUPC", "IlsItemID = ".$ilsItem->ID);
             
             
             if ($ilsItem && $ilsItem->CollectionBook == "Eyelash") {
                $listFields= "Gen";
             } else {
                if($upcs) {
                   if ($upcs->count() == 1) {
                      $finalSize = "SML";
                   } else {
                      $sizeArray = array();
                      foreach ($upcs as $size) {
                         array_push($sizeArray, $size->Size);
                      }
                      if(in_array("32B", $sizeArray)) {
                         $listFields= "Bra";
                      } elseif(in_array("M/L", $sizeArray)) {
                         $listFields= "SMML";
                      } else {
                         $listFields= "SML";
                      }
                      
                   }
                } else {
                   $listFields= "All";
                }
             }
          } else {
             $listFields= "All";
          }
          if($listFields=="OS") {
             return new FieldSet(
                new TextField('OneSizeQty')
             );   
          } elseif ($listFields=="SML") {
             return new FieldSet(
                new TextField('SmallQty'),
                new TextField('MediumQty'),
                new TextField('LargeQty')
             );
          } elseif ($listFields=="SMML"){
             return new FieldSet(
                new TextField('SMQty'),
                new TextField('MLQty')
             );
          } elseif ($listFields=="Gen"){
             return new FieldSet(
                new TextField('GenQty')
             );
          } elseif($listFields=="Bra") {
             return new FieldSet(
                new TextField('32AQty'),
                new TextField('32BQty'),
                new TextField('32CQty'),
                new TextField('34BQty'),
                new TextField('34CQty'),
                new TextField('34DQty'),
                new TextField('34DDQty'),
                new TextField('36BQty'),
                new TextField('36CQty'),
                new TextField('36DQty'),
                new TextField('36DDQty'),
                new TextField('38BQty'),
                new TextField('38CQty'),
                new TextField('38DQty'),
                new TextField('38DDQty')
             );
          } else {
             return new FieldSet(
                new TextField('OneSizeQty')
             );
          }
          
       }
       
       

    }

    This writes the dataobject okay,but when I click on the parent object to edit I get a list of All the BundleQty s with a radial select. Selecting one and saving doesn't write the relation either.

    Any help would be appreciated. Please excuse the hacky code

    Cheers

    475 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.