Cake\ORM\Association\BelongsToMany::junction PHP Method

junction() public method

Sets the table instance for the junction relation. If no arguments are passed, the current configured table instance is returned
public junction ( string | Table | null $table = null ) : Table
$table string | Cake\ORM\Table | null Name or instance for the join table
return Cake\ORM\Table
    public function junction($table = null)
    {
        if ($table === null && $this->_junctionTable) {
            return $this->_junctionTable;
        }
        $tableLocator = $this->tableLocator();
        if ($table === null && $this->_through) {
            $table = $this->_through;
        } elseif ($table === null) {
            $tableName = $this->_junctionTableName();
            $tableAlias = Inflector::camelize($tableName);
            $config = [];
            if (!$tableLocator->exists($tableAlias)) {
                $config = ['table' => $tableName];
                // Propagate the connection if we'll get an auto-model
                if (!App::className($tableAlias, 'Model/Table', 'Table')) {
                    $config['connection'] = $this->source()->connection();
                }
            }
            $table = $tableLocator->get($tableAlias, $config);
        }
        if (is_string($table)) {
            $table = $tableLocator->get($table);
        }
        $source = $this->source();
        $target = $this->target();
        $this->_generateSourceAssociations($table, $source);
        $this->_generateTargetAssociations($table, $source, $target);
        $this->_generateJunctionAssociations($table, $source, $target);
        return $this->_junctionTable = $table;
    }

Usage Example

コード例 #1
0
ファイル: BelongsToManyTest.php プロジェクト: rashmi/newrepo
 /**
  * Tests that custom foreignKeys are properly trasmitted to involved associations
  * when they are customized
  *
  * @return void
  */
 public function testJunctionWithCustomForeignKeys()
 {
     $assoc = new BelongsToMany('Test', ['sourceTable' => $this->article, 'targetTable' => $this->tag, 'foreignKey' => 'Art', 'targetForeignKey' => 'Tag']);
     $junction = $assoc->junction();
     $this->assertEquals('Art', $junction->association('Articles')->foreignKey());
     $this->assertEquals('Tag', $junction->association('Tags')->foreignKey());
     $inverseRelation = $this->tag->association('Articles');
     $this->assertEquals('Tag', $inverseRelation->foreignKey());
     $this->assertEquals('Art', $inverseRelation->targetForeignKey());
 }