Bake\Shell\Task\ModelTask::bakeTable PHP Method

bakeTable() public method

Bake a table class.
public bakeTable ( Table $model, array $data = [] ) : string | null
$model Cake\ORM\Table Model name or object
$data array An array to use to generate the Table
return string | null
    public function bakeTable($model, array $data = [])
    {
        if (!empty($this->params['no-table'])) {
            return null;
        }
        $namespace = Configure::read('App.namespace');
        $pluginPath = '';
        if ($this->plugin) {
            $namespace = $this->_pluginNamespace($this->plugin);
        }
        $name = $model->alias();
        $entity = $this->_entityName($model->alias());
        $data += ['plugin' => $this->plugin, 'pluginPath' => $pluginPath, 'namespace' => $namespace, 'name' => $name, 'entity' => $entity, 'associations' => [], 'primaryKey' => 'id', 'displayField' => null, 'table' => null, 'validation' => [], 'rulesChecker' => [], 'behaviors' => [], 'connection' => $this->connection];
        $this->BakeTemplate->set($data);
        $out = $this->BakeTemplate->generate('Model/table');
        $path = $this->getPath();
        $filename = $path . 'Table' . DS . $name . 'Table.php';
        $this->out("\n" . sprintf('Baking table class for %s...', $name), 1, Shell::QUIET);
        $this->createFile($filename, $out);
        // Work around composer caching that classes/files do not exist.
        // Check for the file as it might not exist in tests.
        if (file_exists($filename)) {
            require_once $filename;
        }
        TableRegistry::clear();
        $emptyFile = $path . 'Table' . DS . 'empty';
        $this->_deleteEmptyFile($emptyFile);
        return $out;
    }

Usage Example

示例#1
0
 /**
  * Tests baking a table with rules
  *
  * @return void
  */
 public function testBakeWithRules()
 {
     $model = TableRegistry::get('Users');
     $associations = ['belongsTo' => [['alias' => 'Countries', 'foreignKey' => 'country_id'], ['alias' => 'Sites', 'foreignKey' => 'site_id']], 'hasMany' => [['alias' => 'BakeComments', 'foreignKey' => 'bake_user_id']]];
     $rulesChecker = $this->Task->getRules($model, $associations);
     $result = $this->Task->bakeTable($model, compact('rulesChecker'));
     $this->assertSameAsFile(__FUNCTION__ . '.php', $result);
 }
All Usage Examples Of Bake\Shell\Task\ModelTask::bakeTable