public function getLinks()
    {

        if( isset( $this->links ) ) {
            return $this->links;
        }

        // build links
        $this->links = array();

        foreach( $this->getAttributes() as $column ) {

            /* This relation has a link if it has a attribute which has all of the following
             *   1. Is unique and is referenced by another attribute
                    TODO. Pete: We don't currently support dual column foreign key constraints
             *   2. This another attribute is one half of a dualcolumn primary key
             *      TODO. Pete: I think this can be relaxed a little. We just need a unique constraint which references two tables
             *   3. The other half of the primary references another unique constraint in another table
             */
            // See 1.
            if( $column->isUnique() and ( $references = $column->getIsReferencedBy() ) ) {

                foreach( $references as $reference ) {

                    // See 2.
                    if( $reference->isPrimaryKey() and count( $primaryKeys = $reference->getRelation()->getPrimaryKeys() ) == 2 ) {

                        $primaryKeys->remove( $reference );
                        $otherHalfofPrimaryKey = $primaryKeys->pop();

                        // See 3.
                        $otherReferences = $otherHalfofPrimaryKey->getReferences();
                        $foreignEntities = new BaseContainer();
                        foreach( $otherReferences as $otherReference ) {

                            // Pete. We might not need the unique check here because foreign keys already have uniqueness enforced
                            if( $otherReference->isUnique() and $otherReference->getRelation() !== $this ) {

                                $foreignEntities[] = $foreignEntities->add( $otherReference->getRelation() );
                                $foreignEntityReference = $otherReference;

                            }

                        }

                        // have we a link?
                        if( $foreignEntities ) {

                            $sourceEntity = $this->getEntityName();
                            $linkEntity = $otherHalfofPrimaryKey->getRelation()->getEntityName();

                            $refSource = array( $column->name, $reference->name );
                            $refForeign = array( $otherHalfofPrimaryKey->name, $foreignEntityReference->name );

                            // get columns and look for a ranking columns
                            $possibleRankingAttributes = $otherHalfofPrimaryKey->getRelation()->getAttributes();
                            $possibleRankingAttributes->remove( $otherHalfofPrimaryKey, $reference );

                            $sortColumn = null;
                            $rankingRegex = "/^{$otherHalfofPrimaryKey->name}_?(r|rank|ranking)$/i";
                            foreach( $possibleRankingAttributes as $ranking ) {

                                if( $ranking->getType()->isNumeric() and preg_match( $rankingRegex, $ranking->name ) ) {
                                    $sortColumn = $ranking->name;
                                    break;
                                }

                            }

                            // sort foreignEntities, we want 'root' entity first then alphabetical
                            $foreignEntities = $foreignEntities
                                ->sort(
                                    function($a, $b){
                                        if( $a->isInherited() and !$b->isInherited() ) {
                                            return 1;
                                        }
                                        return $a->getEntityName() < $b->getEntityName() ? -1 : 1;
                                    }
                                )
                                ->map(function ($e) {
                                    return $e->getEntityName();
                                });

                            $this->links[$linkEntity] = new Link(
                                $sourceEntity,
                                $linkEntity,
                                array_values( $foreignEntities ),
                                $refSource,
                                $refForeign,
                                $sortColumn
                            );

                        }

                    }

                }

            }

        }

        return $this->links;

    }
public function getLinks()
    {

        if( isset( $this->links ) ) {
            return $this->links;
        }

        // build links
        $this->links = array();

        foreach( $this->getAttributes() as $column ) {

            /* This relation has a link if it has a attribute which has all of the following
             *   1. Is unique and is referenced by another attribute
                    TODO. Pete: We don't currently support dual column foreign key constraints
             *   2. This another attribute is one half of a dualcolumn primary key
             *      TODO. Pete: I think this can be relaxed a little. We just need a unique constraint which references two tables
             *   3. The other half of the primary references another unique constraint in another table
             */
            // See 1.
            if( $column->isUnique() and ( $references = $column->getIsReferencedBy() ) ) {

                foreach( $references as $reference ) {

                    // See 2.
                    if( $reference->isPrimaryKey() and count( $primaryKeys = $reference->getRelation()->getPrimaryKeys() ) == 2 ) {

                        $primaryKeys->remove( $reference );
                        $otherHalfofPrimaryKey = $primaryKeys->pop();

                        // See 3.
                        $otherReferences = $otherHalfofPrimaryKey->getReferences();
                        $foreignEntities = new BaseContainer();
                        foreach( $otherReferences as $otherReference ) {

                            // Pete. We might not need the unique check here because foreign keys already have uniqueness enforced
                            if( $otherReference->isUnique() and $otherReference->getRelation() !== $this ) {

                                $foreignEntities[] = $foreignEntities->add( $otherReference->getRelation() );
                                $foreignEntityReference = $otherReference;

                            }

                        }

                        // have we a link?
                        if( $foreignEntities ) {

                            $sourceEntity = $this->getEntityName();
                            $linkEntity = $otherHalfofPrimaryKey->getRelation()->getEntityName();

                            $refSource = array( $column->name, $reference->name );
                            $refForeign = array( $otherHalfofPrimaryKey->name, $foreignEntityReference->name );

                            // get columns and look for a ranking columns
                            $possibleRankingAttributes = $otherHalfofPrimaryKey->getRelation()->getAttributes();
                            $possibleRankingAttributes->remove( $otherHalfofPrimaryKey, $reference );

                            $sortColumn = null;
                            $rankingRegex = "/^{$otherHalfofPrimaryKey->name}_?(r|rank|ranking)$/i";
                            foreach( $possibleRankingAttributes as $ranking ) {

                                if( $ranking->getType()->isNumeric() and preg_match( $rankingRegex, $ranking->name ) ) {
                                    $sortColumn = $ranking->name;
                                    break;
                                }

                            }

                            // sort foreignEntities, we want 'root' entity first then alphabetical
                            $foreignEntities = $foreignEntities
                                ->sort(
                                    function($a, $b){
                                        if( $a->isInherited() and !$b->isInherited() ) {
                                            return 1;
                                        }
                                        return $a->getEntityName() < $b->getEntityName() ? -1 : 1;
                                    }
                                )
                                ->map(function ($e) {
                                    return $e->getEntityName();
                                });

                            $this->links[$linkEntity] = new Link(
                                $sourceEntity,
                                $linkEntity,
                                array_values( $foreignEntities ),
                                $refSource,
                                $refForeign,
                                $sortColumn
                            );

                        }

                    }

                }

            }

        }

        return $this->links;

    }
public function getLinks ( ) { if ( isset ( $ this -> links ) ) { return $ this -> links ; } // build links $ this -> links = array ( ) ; foreach ( $ this -> getAttributes ( ) as $ column ) { /* This relation has a link if it has a attribute which has all of the following
             *   1. Is unique and is referenced by another attribute
                    TODO. Pete: We don't currently support dual column foreign key constraints
             *   2. This another attribute is one half of a dualcolumn primary key
             *      TODO. Pete: I think this can be relaxed a little. We just need a unique constraint which references two tables
             *   3. The other half of the primary references another unique constraint in another table
             */ // See 1. if ( $ column -> isUnique ( ) and ( $ references = $ column -> getIsReferencedBy ( ) ) ) { foreach ( $ references as $ reference ) { // See 2. if ( $ reference -> isPrimaryKey ( ) and count ( $ primaryKeys = $ reference -> getRelation ( ) -> getPrimaryKeys ( ) ) == 2 ) { $ primaryKeys -> remove ( $ reference ) ; $ otherHalfofPrimaryKey = $ primaryKeys -> pop ( ) ; // See 3. $ otherReferences = $ otherHalfofPrimaryKey -> getReferences ( ) ; $ foreignEntities = new BaseContainer ( ) ; foreach ( $ otherReferences as $ otherReference ) { // Pete. We might not need the unique check here because foreign keys already have uniqueness enforced if ( $ otherReference -> isUnique ( ) and $ otherReference -> getRelation ( ) !== $ this ) { $ foreignEntities [ ] = $ foreignEntities -> add ( $ otherReference -> getRelation ( ) ) ; $ foreignEntityReference = $ otherReference ; } } // have we a link? if ( $ foreignEntities ) { $ sourceEntity = $ this -> getEntityName ( ) ; $ linkEntity = $ otherHalfofPrimaryKey -> getRelation ( ) -> getEntityName ( ) ; $ refSource = array ( $ column -> name , $ reference -> name ) ; $ refForeign = array ( $ otherHalfofPrimaryKey -> name , $ foreignEntityReference -> name ) ; // get columns and look for a ranking columns $ possibleRankingAttributes = $ otherHalfofPrimaryKey -> getRelation ( ) -> getAttributes ( ) ; $ possibleRankingAttributes -> remove ( $ otherHalfofPrimaryKey , $ reference ) ; $ sortColumn = null ; $ rankingRegex = "/^{$otherHalfofPrimaryKey->name}_?(r|rank|ranking)$/i" ; foreach ( $ possibleRankingAttributes as $ ranking ) { if ( $ ranking -> getType ( ) -> isNumeric ( ) and preg_match ( $ rankingRegex , $ ranking -> name ) ) { $ sortColumn = $ ranking -> name ; break ; } } // sort foreignEntities, we want 'root' entity first then alphabetical $ foreignEntities = $ foreignEntities -> sort ( function ( $ a , $ b ) { if ( $ a -> isInherited ( ) and ! $ b -> isInherited ( ) ) { return 1 ; } return $ a -> getEntityName ( ) < $ b -> getEntityName ( ) ? - 1 : 1 ; } ) -> map ( function ( $ e ) { return $ e -> getEntityName ( ) ; } ) ; $ this -> links [ $ linkEntity ] = new Link ( $ sourceEntity , $ linkEntity , array_values ( $ foreignEntities ) , $ refSource , $ refForeign , $ sortColumn ) ; } } } } } return $ this -> links ; }
Get array of links to this table
@return array
Get array of links to this table