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

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

Find the hasMany relations and add them to associations list
public findHasMany ( Table $model, array $associations ) : array
$model Cake\ORM\Table Model instance being generated
$associations array Array of in progress associations
Результат array Associations with hasMany added in.
    public function findHasMany($model, array $associations)
    {
        $schema = $model->schema();
        $primaryKey = (array) $schema->primaryKey();
        $tableName = $schema->name();
        $foreignKey = $this->_modelKey($tableName);
        $tables = $this->listAll();
        foreach ($tables as $otherTableName) {
            $otherModel = $this->getTableObject($this->_camelize($otherTableName), $otherTableName);
            $otherSchema = $otherModel->schema();
            $pregTableName = preg_quote($tableName, '/');
            $pregPattern = "/^{$pregTableName}_|_{$pregTableName}\$/";
            if (preg_match($pregPattern, $otherTableName) === 1) {
                $possibleHABTMTargetTable = preg_replace($pregPattern, '', $otherTableName);
                if (in_array($possibleHABTMTargetTable, $tables)) {
                    continue;
                }
            }
            foreach ($otherSchema->columns() as $fieldName) {
                $assoc = false;
                if (!in_array($fieldName, $primaryKey) && $fieldName === $foreignKey) {
                    $assoc = ['alias' => $otherModel->alias(), 'foreignKey' => $fieldName];
                } elseif ($otherTableName === $tableName && $fieldName === 'parent_id') {
                    $className = $this->plugin ? $this->plugin . '.' . $model->alias() : $model->alias();
                    $assoc = ['alias' => 'Child' . $model->alias(), 'className' => $className, 'foreignKey' => $fieldName];
                }
                if ($assoc && $this->plugin && empty($assoc['className'])) {
                    $assoc['className'] = $this->plugin . '.' . $assoc['alias'];
                }
                if ($assoc) {
                    $associations['hasMany'][] = $assoc;
                }
            }
        }
        return $associations;
    }

Usage Example

Пример #1
0
 /**
  * test that hasOne and/or hasMany relations are generated properly.
  *
  * @return void
  */
 public function testHasManyGeneration()
 {
     $this->Task->connection = 'test';
     $model = TableRegistry::get('BakeArticles');
     $result = $this->Task->findHasMany($model, []);
     $expected = ['hasMany' => [['alias' => 'BakeComments', 'foreignKey' => 'bake_article_id']]];
     $this->assertEquals($expected, $result);
     $model = TableRegistry::get('CategoryThreads');
     $result = $this->Task->findHasMany($model, []);
     $expected = ['hasMany' => [['alias' => 'ChildCategoryThreads', 'className' => 'CategoryThreads', 'foreignKey' => 'parent_id']]];
     $this->assertEquals($expected, $result);
     $this->Task->plugin = 'Blog';
     $result = $this->Task->findHasMany($model, []);
     $expected = ['hasMany' => [['alias' => 'ChildCategoryThreads', 'className' => 'Blog.CategoryThreads', 'foreignKey' => 'parent_id']]];
     $this->assertEquals($expected, $result);
 }