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

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

Composer's class cache prevents us from loading the newly generated class. Applying associations if we have a generic table object means fields will be detected correctly.
public applyAssociations ( Table $model, array $associations ) : void
$model Cake\ORM\Table The table to apply associations to.
$associations array The associations to append.
Результат void
    public function applyAssociations($model, $associations)
    {
        if (get_class($model) !== 'Cake\\ORM\\Table') {
            return;
        }
        foreach ($associations as $type => $assocs) {
            foreach ($assocs as $assoc) {
                $alias = $assoc['alias'];
                unset($assoc['alias']);
                $model->{$type}($alias, $assoc);
            }
        }
    }

Usage Example

Пример #1
0
 /**
  * Test applying associations does nothing on a concrete class
  *
  * @return void
  */
 public function testApplyAssociationsConcreteClass()
 {
     Configure::write('App.namespace', 'Bake\\Test\\App');
     $articles = TableRegistry::get('Articles');
     $assocs = ['belongsTo' => [['alias' => 'BakeUsers', 'foreignKey' => 'bake_user_id']], 'hasMany' => [['alias' => 'BakeComments', 'foreignKey' => 'bake_article_id']], 'belongsToMany' => [['alias' => 'BakeTags', 'foreignKey' => 'bake_article_id', 'joinTable' => 'bake_articles_bake_tags', 'targetForeignKey' => 'bake_tag_id']]];
     $original = $articles->associations()->keys();
     $this->Task->applyAssociations($articles, $assocs);
     $new = $articles->associations()->keys();
     $this->assertEquals($original, $new);
 }