当前位置: 首页>>代码示例>>PHP>>正文


PHP DataObject::hasOne方法代码示例

本文整理汇总了PHP中SilverStripe\ORM\DataObject::hasOne方法的典型用法代码示例。如果您正苦于以下问题:PHP DataObject::hasOne方法的具体用法?PHP DataObject::hasOne怎么用?PHP DataObject::hasOne使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SilverStripe\ORM\DataObject的用法示例。


在下文中一共展示了DataObject::hasOne方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: duplicateManyManyRelations

 /**
  * Copies the many_many and belongs_many_many relations from one object to another instance of the name of object
  * The destinationObject must be written to the database already and have an ID. Writing is performed
  * automatically when adding the new relations.
  *
  * @param DataObject $sourceObject the source object to duplicate from
  * @param DataObject $destinationObject the destination object to populate with the duplicated relations
  * @return DataObject with the new many_many relations copied in
  */
 protected function duplicateManyManyRelations($sourceObject, $destinationObject)
 {
     if (!$destinationObject || $destinationObject->ID < 1) {
         user_error("Can't duplicate relations for an object that has not been written to the database", E_USER_ERROR);
     }
     //duplicate complex relations
     // DO NOT copy has_many relations, because copying the relation would result in us changing the has_one
     // relation on the other side of this relation to point at the copy and no longer the original (being a
     // has_one, it can only point at one thing at a time). So, all relations except has_many can and are copied
     if ($sourceObject->hasOne()) {
         foreach ($sourceObject->hasOne() as $name => $type) {
             $this->duplicateRelations($sourceObject, $destinationObject, $name);
         }
     }
     if ($sourceObject->manyMany()) {
         foreach ($sourceObject->manyMany() as $name => $type) {
             //many_many include belongs_many_many
             $this->duplicateRelations($sourceObject, $destinationObject, $name);
         }
     }
     return $destinationObject;
 }
开发者ID:SpiritLevel,项目名称:silverstripe-framework,代码行数:31,代码来源:DataObject.php

示例2: getFieldList

 /**
  * Gets the form fields as defined through the metadata
  * on {@link $obj} and the custom parameters passed to FormScaffolder.
  * Depending on those parameters, the fields can be used in ajax-context,
  * contain {@link TabSet}s etc.
  *
  * @return FieldList
  */
 public function getFieldList()
 {
     $fields = new FieldList();
     // tabbed or untabbed
     if ($this->tabbed) {
         $fields->push(new TabSet("Root", $mainTab = new Tab("Main")));
         $mainTab->setTitle(_t('SiteTree.TABMAIN', "Main"));
     }
     // Add logical fields directly specified in db config
     foreach ($this->obj->config()->db as $fieldName => $fieldType) {
         // Skip restricted fields
         if ($this->restrictFields && !in_array($fieldName, $this->restrictFields)) {
             continue;
         }
         // @todo Pass localized title
         if ($this->fieldClasses && isset($this->fieldClasses[$fieldName])) {
             $fieldClass = $this->fieldClasses[$fieldName];
             $fieldObject = new $fieldClass($fieldName);
         } else {
             $fieldObject = $this->obj->dbObject($fieldName)->scaffoldFormField(null, $this->getParamsArray());
         }
         // Allow fields to opt-out of scaffolding
         if (!$fieldObject) {
             continue;
         }
         $fieldObject->setTitle($this->obj->fieldLabel($fieldName));
         if ($this->tabbed) {
             $fields->addFieldToTab("Root.Main", $fieldObject);
         } else {
             $fields->push($fieldObject);
         }
     }
     // add has_one relation fields
     if ($this->obj->hasOne()) {
         foreach ($this->obj->hasOne() as $relationship => $component) {
             if ($this->restrictFields && !in_array($relationship, $this->restrictFields)) {
                 continue;
             }
             $fieldName = $component === 'SilverStripe\\ORM\\DataObject' ? $relationship : "{$relationship}ID";
             if ($this->fieldClasses && isset($this->fieldClasses[$fieldName])) {
                 $fieldClass = $this->fieldClasses[$fieldName];
                 $hasOneField = new $fieldClass($fieldName);
             } else {
                 $hasOneField = $this->obj->dbObject($fieldName)->scaffoldFormField(null, $this->getParamsArray());
             }
             if (empty($hasOneField)) {
                 continue;
             }
             // Allow fields to opt out of scaffolding
             $hasOneField->setTitle($this->obj->fieldLabel($relationship));
             if ($this->tabbed) {
                 $fields->addFieldToTab("Root.Main", $hasOneField);
             } else {
                 $fields->push($hasOneField);
             }
         }
     }
     // only add relational fields if an ID is present
     if ($this->obj->ID) {
         // add has_many relation fields
         if ($this->obj->hasMany() && ($this->includeRelations === true || isset($this->includeRelations['has_many']))) {
             foreach ($this->obj->hasMany() as $relationship => $component) {
                 if ($this->tabbed) {
                     $fields->findOrMakeTab("Root.{$relationship}", $this->obj->fieldLabel($relationship));
                 }
                 $fieldClass = isset($this->fieldClasses[$relationship]) ? $this->fieldClasses[$relationship] : 'SilverStripe\\Forms\\GridField\\GridField';
                 /** @var GridField $grid */
                 $grid = Object::create($fieldClass, $relationship, $this->obj->fieldLabel($relationship), $this->obj->{$relationship}(), GridFieldConfig_RelationEditor::create());
                 if ($this->tabbed) {
                     $fields->addFieldToTab("Root.{$relationship}", $grid);
                 } else {
                     $fields->push($grid);
                 }
             }
         }
         if ($this->obj->manyMany() && ($this->includeRelations === true || isset($this->includeRelations['many_many']))) {
             foreach ($this->obj->manyMany() as $relationship => $component) {
                 if ($this->tabbed) {
                     $fields->findOrMakeTab("Root.{$relationship}", $this->obj->fieldLabel($relationship));
                 }
                 $fieldClass = isset($this->fieldClasses[$relationship]) ? $this->fieldClasses[$relationship] : 'SilverStripe\\Forms\\GridField\\GridField';
                 /** @var GridField $grid */
                 $grid = Object::create($fieldClass, $relationship, $this->obj->fieldLabel($relationship), $this->obj->{$relationship}(), GridFieldConfig_RelationEditor::create());
                 if ($this->tabbed) {
                     $fields->addFieldToTab("Root.{$relationship}", $grid);
                 } else {
                     $fields->push($grid);
                 }
             }
         }
     }
     return $fields;
//.........这里部分代码省略.........
开发者ID:SpiritLevel,项目名称:silverstripe-framework,代码行数:101,代码来源:FormScaffolder.php


注:本文中的SilverStripe\ORM\DataObject::hasOne方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。