コード例 #1
0
 /**
  * Populate relations for items of the dataClass (code moved from writeDataObject).
  */
 protected function writeRelations($dataClass, $items)
 {
     $testDataTag = basename($this->fixtureFile);
     foreach ($items as $identifier => $fields) {
         $tag = $this->getTag($testDataTag, $dataClass, $identifier);
         $obj = null;
         if ($tag) {
             $obj = $tag->getObject();
         }
         if (!$obj) {
             Controller::curr()->message("<br>(Could not find {$dataClass}::{$identifier} for relation updates, skipping)");
             continue;
         }
         // Populate all relations
         if ($fields) {
             foreach ($fields as $fieldName => $fieldVal) {
                 if ($obj->many_many($fieldName) || $obj->has_many($fieldName)) {
                     $parsedItems = array();
                     $items = preg_split('/ *, */', trim($fieldVal));
                     foreach ($items as $item) {
                         $parsedItems[] = $this->parseFixtureVal($item);
                     }
                     $obj->{$fieldName}()->setByIDList($parsedItems);
                 } elseif ($obj->has_one($fieldName)) {
                     if ($fieldName == 'Parent' && $obj->URLSegment) {
                         // If we are changing the parent, nullify the URLSegment so the system can get rid of suffixes.
                         $obj->URLSegment = null;
                     }
                     $obj->{$fieldName . 'ID'} = $this->parseFixtureVal($fieldVal);
                     $obj->write();
                     TestDataYamlFixture::attempt_publish($obj);
                 }
             }
         }
     }
 }