src/ORM/DataObject.php
public function getManyManyComponents($componentName, $id = null)
    {
        if (!isset($id)) {
            $id = $this->ID;
        }
        $schema = static::getSchema();
        $manyManyComponent = $schema->manyManyComponent(static::class, $componentName);
        if (!$manyManyComponent) {
            throw new InvalidArgumentException(sprintf(
                "DataObject::getComponents(): Unknown many-to-many component '%s' on class '%s'",
                $componentName,
                static::class
            ));
        }

        // If we haven't been written yet, we can't save these relations, so use a list that handles this case
        if (!$id) {
            if (!isset($this->unsavedRelations[$componentName])) {
                $this->unsavedRelations[$componentName] =
                    new UnsavedRelationList(
                        $manyManyComponent['parentClass'],
                        $componentName,
                        $manyManyComponent['childClass']
                    );
            }
            return $this->unsavedRelations[$componentName];
        }

        $extraFields = $schema->manyManyExtraFieldsForComponent(static::class, $componentName) ?: array();
        /** @var RelationList $result */
        $result = Injector::inst()->create(
            $manyManyComponent['relationClass'],
            $manyManyComponent['childClass'],
            $manyManyComponent['join'],
            $manyManyComponent['childField'],
            $manyManyComponent['parentField'],
            $extraFields,
            $manyManyComponent['parentClass'],
            static::class // In case ManyManyThroughList needs to use PolymorphicHasManyList internally
        );

        // Store component data in query meta-data
        $result = $result->alterDataQuery(function ($query) use ($extraFields) {
            /** @var DataQuery $query */
            $query->setQueryParam('Component.ExtraFields', $extraFields);
        });

        // If we have a default sort set for our "join" then we should overwrite any default already set.
        $joinSort = Config::inst()->get($manyManyComponent['join'], 'default_sort');
        if (!empty($joinSort)) {
            $result = $result->sort($joinSort);
        }

        $this->extend('updateManyManyComponents', $result);

        // If this is called on a singleton, then we return an 'orphaned relation' that can have the
        // foreignID set elsewhere.
        return $result
            ->setDataQueryParam($this->getInheritableQueryParams())
            ->forForeignID($id);
    }
public function getManyManyComponents($componentName, $id = null)
    {
        if (!isset($id)) {
            $id = $this->ID;
        }
        $schema = static::getSchema();
        $manyManyComponent = $schema->manyManyComponent(static::class, $componentName);
        if (!$manyManyComponent) {
            throw new InvalidArgumentException(sprintf(
                "DataObject::getComponents(): Unknown many-to-many component '%s' on class '%s'",
                $componentName,
                static::class
            ));
        }

        // If we haven't been written yet, we can't save these relations, so use a list that handles this case
        if (!$id) {
            if (!isset($this->unsavedRelations[$componentName])) {
                $this->unsavedRelations[$componentName] =
                    new UnsavedRelationList(
                        $manyManyComponent['parentClass'],
                        $componentName,
                        $manyManyComponent['childClass']
                    );
            }
            return $this->unsavedRelations[$componentName];
        }

        $extraFields = $schema->manyManyExtraFieldsForComponent(static::class, $componentName) ?: array();
        /** @var RelationList $result */
        $result = Injector::inst()->create(
            $manyManyComponent['relationClass'],
            $manyManyComponent['childClass'],
            $manyManyComponent['join'],
            $manyManyComponent['childField'],
            $manyManyComponent['parentField'],
            $extraFields,
            $manyManyComponent['parentClass'],
            static::class // In case ManyManyThroughList needs to use PolymorphicHasManyList internally
        );

        // Store component data in query meta-data
        $result = $result->alterDataQuery(function ($query) use ($extraFields) {
            /** @var DataQuery $query */
            $query->setQueryParam('Component.ExtraFields', $extraFields);
        });

        // If we have a default sort set for our "join" then we should overwrite any default already set.
        $joinSort = Config::inst()->get($manyManyComponent['join'], 'default_sort');
        if (!empty($joinSort)) {
            $result = $result->sort($joinSort);
        }

        $this->extend('updateManyManyComponents', $result);

        // If this is called on a singleton, then we return an 'orphaned relation' that can have the
        // foreignID set elsewhere.
        return $result
            ->setDataQueryParam($this->getInheritableQueryParams())
            ->forForeignID($id);
    }
public function getManyManyComponents ( $ componentName , $ id = null ) { if ( ! isset ( $ id ) ) { $ id = $ this -> ID ; } $ schema = static :: getSchema ( ) ; $ manyManyComponent = $ schema -> manyManyComponent ( static :: class , $ componentName ) ; if ( ! $ manyManyComponent ) { throw new InvalidArgumentException ( sprintf ( "DataObject::getComponents(): Unknown many-to-many component '%s' on class '%s'" , $ componentName , static :: class ) ) ; } // If we haven't been written yet, we can't save these relations, so use a list that handles this case if ( ! $ id ) { if ( ! isset ( $ this -> unsavedRelations [ $ componentName ] ) ) { $ this -> unsavedRelations [ $ componentName ] = new UnsavedRelationList ( $ manyManyComponent [ 'parentClass' ] , $ componentName , $ manyManyComponent [ 'childClass' ] ) ; } return $ this -> unsavedRelations [ $ componentName ] ; } $ extraFields = $ schema -> manyManyExtraFieldsForComponent ( static :: class , $ componentName ) ? : array ( ) ; /** @var RelationList $result */ $ result = Injector :: inst ( ) -> create ( $ manyManyComponent [ 'relationClass' ] , $ manyManyComponent [ 'childClass' ] , $ manyManyComponent [ 'join' ] , $ manyManyComponent [ 'childField' ] , $ manyManyComponent [ 'parentField' ] , $ extraFields , $ manyManyComponent [ 'parentClass' ] , static :: class // In case ManyManyThroughList needs to use PolymorphicHasManyList internally ) ; // Store component data in query meta-data $ result = $ result -> alterDataQuery ( function ( $ query ) use ( $ extraFields ) { /** @var DataQuery $query */ $ query -> setQueryParam ( 'Component.ExtraFields' , $ extraFields ) ; } ) ; // If we have a default sort set for our "join" then we should overwrite any default already set. $ joinSort = Config :: inst ( ) -> get ( $ manyManyComponent [ 'join' ] , 'default_sort' ) ; if ( ! empty ( $ joinSort ) ) { $ result = $ result -> sort ( $ joinSort ) ; } $ this -> extend ( 'updateManyManyComponents' , $ result ) ; // If this is called on a singleton, then we return an 'orphaned relation' that can have the // foreignID set elsewhere. return $ result -> setDataQueryParam ( $ this -> getInheritableQueryParams ( ) ) -> forForeignID ( $ id ) ; }
Returns a many-to-many component, as a ManyManyList.
@param string $componentName Name of the many-many component
@param int|array $id Optional ID for parent of this relation, if not the current record
@return ManyManyList|UnsavedRelationList The set of components
Returns a many - to - many component as a ManyManyList .