public function manyToMany(
        Table $toTable,
        Table $pivot,
        $name = null,
        $toTableColumn = null,
        $localColumn = null
    ) : Table {
        $pivotColumns = $pivot->getColumns();

        $keymap = [];
        if (!isset($toTableColumn)) {
            $toTableColumn = [];
        }
        if (!is_array($toTableColumn)) {
            $toTableColumn = [$toTableColumn];
        }
        foreach ($this->getPrimaryKey() as $k => $pkField) {
            if (isset($toTableColumn[$pkField])) {
                $key = $toTableColumn[$pkField];
            } elseif (isset($toTableColumn[$k])) {
                $key = $toTableColumn[$k];
            } else {
                $key = $this->getName().'_'.$pkField;
            }
            if (!in_array($key, $pivotColumns)) {
                throw new DBException('Missing foreign key mapping');
            }
            $keymap[$pkField] = $key;
        }

        $pivotKeymap = [];
        if (!isset($localColumn)) {
            $localColumn = [];
        }
        if (!is_array($localColumn)) {
            $localColumn = [$localColumn];
        }
        foreach ($toTable->getPrimaryKey() as $k => $pkField) {
            if (isset($localColumn[$pkField])) {
                $key = $localColumn[$pkField];
            } elseif (isset($localColumn[$k])) {
                $key = $localColumn[$k];
            } else {
                $key = $toTable->getName().'_'.$pkField;
            }
            if (!in_array($key, $pivotColumns)) {
                throw new DBException('Missing foreign key mapping');
            }
            $pivotKeymap[$key] = $pkField;
        }

        if (!isset($name)) {
            $name = $toTable->getName().'_'.implode('_', array_keys($keymap));
        }
        $this->addRelation(new TableRelation(
            $name,
            $toTable,
            $keymap,
            true,
            $pivot,
            $pivotKeymap
        ));
        return $this;
    }
public function manyToMany(
        Table $toTable,
        Table $pivot,
        $name = null,
        $toTableColumn = null,
        $localColumn = null
    ) : Table {
        $pivotColumns = $pivot->getColumns();

        $keymap = [];
        if (!isset($toTableColumn)) {
            $toTableColumn = [];
        }
        if (!is_array($toTableColumn)) {
            $toTableColumn = [$toTableColumn];
        }
        foreach ($this->getPrimaryKey() as $k => $pkField) {
            if (isset($toTableColumn[$pkField])) {
                $key = $toTableColumn[$pkField];
            } elseif (isset($toTableColumn[$k])) {
                $key = $toTableColumn[$k];
            } else {
                $key = $this->getName().'_'.$pkField;
            }
            if (!in_array($key, $pivotColumns)) {
                throw new DBException('Missing foreign key mapping');
            }
            $keymap[$pkField] = $key;
        }

        $pivotKeymap = [];
        if (!isset($localColumn)) {
            $localColumn = [];
        }
        if (!is_array($localColumn)) {
            $localColumn = [$localColumn];
        }
        foreach ($toTable->getPrimaryKey() as $k => $pkField) {
            if (isset($localColumn[$pkField])) {
                $key = $localColumn[$pkField];
            } elseif (isset($localColumn[$k])) {
                $key = $localColumn[$k];
            } else {
                $key = $toTable->getName().'_'.$pkField;
            }
            if (!in_array($key, $pivotColumns)) {
                throw new DBException('Missing foreign key mapping');
            }
            $pivotKeymap[$key] = $pkField;
        }

        if (!isset($name)) {
            $name = $toTable->getName().'_'.implode('_', array_keys($keymap));
        }
        $this->addRelation(new TableRelation(
            $name,
            $toTable,
            $keymap,
            true,
            $pivot,
            $pivotKeymap
        ));
        return $this;
    }
public function manyToMany ( Table $ toTable , Table $ pivot , $ name = null , $ toTableColumn = null , $ localColumn = null ) : Table { $ pivotColumns = $ pivot -> getColumns ( ) ; $ keymap = [ ] ; if ( ! isset ( $ toTableColumn ) ) { $ toTableColumn = [ ] ; } if ( ! is_array ( $ toTableColumn ) ) { $ toTableColumn = [ $ toTableColumn ] ; } foreach ( $ this -> getPrimaryKey ( ) as $ k => $ pkField ) { if ( isset ( $ toTableColumn [ $ pkField ] ) ) { $ key = $ toTableColumn [ $ pkField ] ; } elseif ( isset ( $ toTableColumn [ $ k ] ) ) { $ key = $ toTableColumn [ $ k ] ; } else { $ key = $ this -> getName ( ) . '_' . $ pkField ; } if ( ! in_array ( $ key , $ pivotColumns ) ) { throw new DBException ( 'Missing foreign key mapping' ) ; } $ keymap [ $ pkField ] = $ key ; } $ pivotKeymap = [ ] ; if ( ! isset ( $ localColumn ) ) { $ localColumn = [ ] ; } if ( ! is_array ( $ localColumn ) ) { $ localColumn = [ $ localColumn ] ; } foreach ( $ toTable -> getPrimaryKey ( ) as $ k => $ pkField ) { if ( isset ( $ localColumn [ $ pkField ] ) ) { $ key = $ localColumn [ $ pkField ] ; } elseif ( isset ( $ localColumn [ $ k ] ) ) { $ key = $ localColumn [ $ k ] ; } else { $ key = $ toTable -> getName ( ) . '_' . $ pkField ; } if ( ! in_array ( $ key , $ pivotColumns ) ) { throw new DBException ( 'Missing foreign key mapping' ) ; } $ pivotKeymap [ $ key ] = $ pkField ; } if ( ! isset ( $ name ) ) { $ name = $ toTable -> getName ( ) . '_' . implode ( '_' , array_keys ( $ keymap ) ) ; } $ this -> addRelation ( new TableRelation ( $ name , $ toTable , $ keymap , true , $ pivot , $ pivotKeymap ) ) ; return $ this ; }
Create a relation where each record has many linked records in another table but using a liking table
@param  Table   $toTable       the related table definition
@param  Table   $pivot         the pivot table definition
@param  string|null       $name          the name of the relation (defaults to the related table name)
@param  string|array|null $toTableColumn the local columns pointing to the pivot table
@param  string|array|null $localColumn   the pivot columns pointing to the related table PK
@return $this
Create a relation where each record has many linked records in another table but using a liking table