src/ORM/DataObject.php
public function getComponent($componentName)
    {
        if (isset($this->components[$componentName])) {
            return $this->components[$componentName];
        }

        $schema = static::getSchema();
        if ($class = $schema->hasOneComponent(static::class, $componentName)) {
            $joinField = $componentName . 'ID';
            $joinID = $this->getField($joinField);

            // Extract class name for polymorphic relations
            if ($class === self::class) {
                $class = $this->getField($componentName . 'Class');
                if (empty($class)) {
                    return null;
                }
            }

            if ($joinID) {
                // Ensure that the selected object originates from the same stage, subsite, etc
                $component = DataObject::get($class)
                    ->filter('ID', $joinID)
                    ->setDataQueryParam($this->getInheritableQueryParams())
                    ->first();
            }

            if (empty($component)) {
                $component = Injector::inst()->create($class);
            }
        } elseif ($class = $schema->belongsToComponent(static::class, $componentName)) {
            $joinField = $schema->getRemoteJoinField(static::class, $componentName, 'belongs_to', $polymorphic);
            $joinID = $this->ID;

            if ($joinID) {
                // Prepare filter for appropriate join type
                if ($polymorphic) {
                    $filter = array(
                        "{$joinField}ID" => $joinID,
                        "{$joinField}Class" => static::class,
                    );
                } else {
                    $filter = array(
                        $joinField => $joinID
                    );
                }

                // Ensure that the selected object originates from the same stage, subsite, etc
                $component = DataObject::get($class)
                    ->filter($filter)
                    ->setDataQueryParam($this->getInheritableQueryParams())
                    ->first();
            }

            if (empty($component)) {
                $component = Injector::inst()->create($class);
                if ($polymorphic) {
                    $component->{$joinField . 'ID'} = $this->ID;
                    $component->{$joinField . 'Class'} = static::class;
                } else {
                    $component->$joinField = $this->ID;
                }
            }
        } else {
            throw new InvalidArgumentException(
                "DataObject->getComponent(): Could not find component '$componentName'."
            );
        }

        $this->components[$componentName] = $component;
        return $component;
    }
public function getComponent($componentName)
    {
        if (isset($this->components[$componentName])) {
            return $this->components[$componentName];
        }

        $schema = static::getSchema();
        if ($class = $schema->hasOneComponent(static::class, $componentName)) {
            $joinField = $componentName . 'ID';
            $joinID = $this->getField($joinField);

            // Extract class name for polymorphic relations
            if ($class === self::class) {
                $class = $this->getField($componentName . 'Class');
                if (empty($class)) {
                    return null;
                }
            }

            if ($joinID) {
                // Ensure that the selected object originates from the same stage, subsite, etc
                $component = DataObject::get($class)
                    ->filter('ID', $joinID)
                    ->setDataQueryParam($this->getInheritableQueryParams())
                    ->first();
            }

            if (empty($component)) {
                $component = Injector::inst()->create($class);
            }
        } elseif ($class = $schema->belongsToComponent(static::class, $componentName)) {
            $joinField = $schema->getRemoteJoinField(static::class, $componentName, 'belongs_to', $polymorphic);
            $joinID = $this->ID;

            if ($joinID) {
                // Prepare filter for appropriate join type
                if ($polymorphic) {
                    $filter = array(
                        "{$joinField}ID" => $joinID,
                        "{$joinField}Class" => static::class,
                    );
                } else {
                    $filter = array(
                        $joinField => $joinID
                    );
                }

                // Ensure that the selected object originates from the same stage, subsite, etc
                $component = DataObject::get($class)
                    ->filter($filter)
                    ->setDataQueryParam($this->getInheritableQueryParams())
                    ->first();
            }

            if (empty($component)) {
                $component = Injector::inst()->create($class);
                if ($polymorphic) {
                    $component->{$joinField . 'ID'} = $this->ID;
                    $component->{$joinField . 'Class'} = static::class;
                } else {
                    $component->$joinField = $this->ID;
                }
            }
        } else {
            throw new InvalidArgumentException(
                "DataObject->getComponent(): Could not find component '$componentName'."
            );
        }

        $this->components[$componentName] = $component;
        return $component;
    }
public function getComponent ( $ componentName ) { if ( isset ( $ this -> components [ $ componentName ] ) ) { return $ this -> components [ $ componentName ] ; } $ schema = static :: getSchema ( ) ; if ( $ class = $ schema -> hasOneComponent ( static :: class , $ componentName ) ) { $ joinField = $ componentName . 'ID' ; $ joinID = $ this -> getField ( $ joinField ) ; // Extract class name for polymorphic relations if ( $ class === self :: class ) { $ class = $ this -> getField ( $ componentName . 'Class' ) ; if ( empty ( $ class ) ) { return null ; } } if ( $ joinID ) { // Ensure that the selected object originates from the same stage, subsite, etc $ component = DataObject :: get ( $ class ) -> filter ( 'ID' , $ joinID ) -> setDataQueryParam ( $ this -> getInheritableQueryParams ( ) ) -> first ( ) ; } if ( empty ( $ component ) ) { $ component = Injector :: inst ( ) -> create ( $ class ) ; } } elseif ( $ class = $ schema -> belongsToComponent ( static :: class , $ componentName ) ) { $ joinField = $ schema -> getRemoteJoinField ( static :: class , $ componentName , 'belongs_to' , $ polymorphic ) ; $ joinID = $ this -> ID ; if ( $ joinID ) { // Prepare filter for appropriate join type if ( $ polymorphic ) { $ filter = array ( "{$joinField}ID" => $ joinID , "{$joinField}Class" => static :: class , ) ; } else { $ filter = array ( $ joinField => $ joinID ) ; } // Ensure that the selected object originates from the same stage, subsite, etc $ component = DataObject :: get ( $ class ) -> filter ( $ filter ) -> setDataQueryParam ( $ this -> getInheritableQueryParams ( ) ) -> first ( ) ; } if ( empty ( $ component ) ) { $ component = Injector :: inst ( ) -> create ( $ class ) ; if ( $ polymorphic ) { $ component -> { $ joinField . 'ID' } = $ this -> ID ; $ component -> { $ joinField . 'Class' } = static :: class ; } else { $ component -> $ joinField = $ this -> ID ; } } } else { throw new InvalidArgumentException ( "DataObject->getComponent(): Could not find component '$componentName'." ) ; } $ this -> components [ $ componentName ] = $ component ; return $ component ; }
Return a unary component object from a one to one relationship, as a DataObject.
If no component is available, an 'empty component' will be returned for
non-polymorphic relations, or for polymorphic relations with a class set.

@param string $componentName Name of the component
@return DataObject The component object. It's exact type will be that of the component.
@throws Exception
Return a unary component object from a one to one relationship as a DataObject . If no component is available an empty component will be returned for non - polymorphic relations or for polymorphic relations with a class set .