Bake\Shell\Task\ModelTask::getAssociations PHP Метод

getAssociations() публичный Метод

Get the array of associations to generate.
public getAssociations ( Table $table ) : array
$table Cake\ORM\Table The table to get associations for.
Результат array
    public function getAssociations(Table $table)
    {
        if (!empty($this->params['no-associations'])) {
            return [];
        }
        $this->out('One moment while associations are detected.');
        $this->listAll();
        $associations = ['belongsTo' => [], 'hasMany' => [], 'belongsToMany' => []];
        $primary = $table->primaryKey();
        $associations = $this->findBelongsTo($table, $associations);
        if (is_array($primary) && count($primary) > 1) {
            $this->err('<warning>Bake cannot generate associations for composite primary keys at this time</warning>.');
            return $associations;
        }
        $associations = $this->findHasMany($table, $associations);
        $associations = $this->findBelongsToMany($table, $associations);
        return $associations;
    }

Usage Example

Пример #1
0
 /**
  * Test getAssociations in a plugin
  *
  * @return void
  */
 public function testGetAssociationsPlugin()
 {
     $articles = TableRegistry::get('BakeArticles');
     $this->Task->plugin = 'TestBake';
     $result = $this->Task->getAssociations($articles);
     $expected = ['belongsTo' => [['alias' => 'BakeUsers', 'className' => 'TestBake.BakeUsers', 'foreignKey' => 'bake_user_id', 'joinType' => 'INNER']], 'hasMany' => [['alias' => 'BakeComments', 'className' => 'TestBake.BakeComments', 'foreignKey' => 'bake_article_id']], 'belongsToMany' => [['alias' => 'BakeTags', 'className' => 'TestBake.BakeTags', 'foreignKey' => 'bake_article_id', 'joinTable' => 'bake_articles_bake_tags', 'targetForeignKey' => 'bake_tag_id']]];
     $this->assertEquals($expected, $result);
 }