Migrations\Shell\Task\SeedTask::templateData PHP Method

templateData() public method

Get template data.
public templateData ( ) : array
return array
    public function templateData()
    {
        $namespace = Configure::read('App.namespace');
        if ($this->plugin) {
            $namespace = $this->_pluginNamespace($this->plugin);
        }
        $table = Inflector::tableize($this->args[0]);
        if (!empty($this->params['table'])) {
            $table = $this->params['table'];
        }
        $records = false;
        if ($this->param('data')) {
            $limit = (int) $this->param('limit');
            $fields = $this->param('fields') ?: '*';
            if ($fields !== '*') {
                $fields = explode(',', $fields);
            }
            $connection = ConnectionManager::get($this->connection);
            $query = $connection->newQuery()->from($table)->select($fields);
            if ($limit) {
                $query->limit($limit);
            }
            $records = $connection->execute($query)->fetchAll('assoc');
            $records = $this->prettifyArray($records);
        }
        return ['className' => $this->BakeTemplate->viewVars['name'], 'namespace' => $namespace, 'records' => $records, 'table' => $table];
    }

Usage Example

 /**
  * Data provider for the template. Overridden to include records if needed.
  *
  * @return array
  */
 public function templateData()
 {
     $templateData = parent::templateData();
     if (!empty($this->params['records'])) {
         $modelName = Inflector::camelize($templateData['table']);
         $data = $this->SeedGenerate->getRecordsFromTable($modelName, $templateData['table'])->toArray();
         if (!empty($data)) {
             $templateData['records'] = $this->stringifyRecords($data);
             #debug($templateData);exit;
         }
     }
     return $templateData;
 }