ModelTask.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Console\Command\Task;
  16. use Cake\Console\Shell;
  17. use Cake\Core\App;
  18. use Cake\Core\Configure;
  19. use Cake\Datasource\ConnectionManager;
  20. use Cake\ORM\Table;
  21. use Cake\ORM\TableRegistry;
  22. use Cake\Utility\ClassRegistry;
  23. use Cake\Utility\Inflector;
  24. /**
  25. * Task class for generating model files.
  26. */
  27. class ModelTask extends BakeTask {
  28. /**
  29. * path to Model directory
  30. *
  31. * @var string
  32. */
  33. public $path = null;
  34. /**
  35. * tasks
  36. *
  37. * @var array
  38. */
  39. public $tasks = ['DbConfig', 'Fixture', 'Test', 'Template'];
  40. /**
  41. * Tables to skip when running all()
  42. *
  43. * @var array
  44. */
  45. public $skipTables = ['i18n'];
  46. /**
  47. * Holds tables found on connection.
  48. *
  49. * @var array
  50. */
  51. protected $_tables = [];
  52. /**
  53. * Holds the model names
  54. *
  55. * @var array
  56. */
  57. protected $_modelNames = [];
  58. /**
  59. * Holds validation method map.
  60. *
  61. * @var array
  62. */
  63. protected $_validations = [];
  64. /**
  65. * Override initialize
  66. *
  67. * @return void
  68. */
  69. public function initialize() {
  70. $this->path = APP . '/Model/';
  71. }
  72. /**
  73. * Execution method always used for tasks
  74. *
  75. * @return void
  76. */
  77. public function execute() {
  78. parent::execute();
  79. if (!isset($this->connection)) {
  80. $this->connection = 'default';
  81. }
  82. if (empty($this->args)) {
  83. $this->out(__d('cake_console', 'Choose a model to bake from the following:'));
  84. foreach ($this->listAll() as $table) {
  85. $this->out('- ' . $this->_modelName($table));
  86. }
  87. return true;
  88. }
  89. if (strtolower($this->args[0]) === 'all') {
  90. return $this->all();
  91. }
  92. $this->generate($this->_modelName($this->args[0]));
  93. }
  94. /**
  95. * Generate code for the given model name.
  96. *
  97. * @param string $name The model name to generate.
  98. * @return void
  99. */
  100. public function generate($name) {
  101. $table = $this->getTable($name);
  102. $model = $this->getTableObject($name, $table);
  103. $associations = $this->getAssociations($model);
  104. $primaryKey = $this->getPrimaryKey($model);
  105. $displayField = $this->getDisplayField($model);
  106. $fields = $this->getFields($model);
  107. $validation = $this->getValidation($model);
  108. $behaviors = $this->getBehaviors($model);
  109. $data = compact(
  110. 'associations', 'primaryKey', 'displayField',
  111. 'table', 'fields', 'validation', 'behaviors'
  112. );
  113. $this->bakeTable($model, $data);
  114. $this->bakeEntity($model, $data);
  115. $this->bakeFixture($model->alias(), $table);
  116. $this->bakeTest($model->alias());
  117. }
  118. /**
  119. * Bake all models at once.
  120. *
  121. * @return void
  122. */
  123. public function all() {
  124. $this->listAll($this->connection, false);
  125. foreach ($this->_tables as $table) {
  126. if (in_array($table, $this->skipTables)) {
  127. continue;
  128. }
  129. $modelClass = $this->_modelName($table);
  130. $this->out(__d('cake_console', 'Baking %s', $modelClass));
  131. $this->generate($modelClass);
  132. }
  133. }
  134. /**
  135. * Get a model object for a class name.
  136. *
  137. * @param string $className Name of class you want model to be.
  138. * @param string $table Table name
  139. * @return \Cake\ORM\Table Table instance
  140. */
  141. public function getTableObject($className, $table) {
  142. if (TableRegistry::exists($className)) {
  143. return TableRegistry::get($className);
  144. }
  145. return TableRegistry::get($className, [
  146. 'name' => $className,
  147. 'table' => $table,
  148. 'connection' => ConnectionManager::get($this->connection)
  149. ]);
  150. }
  151. /**
  152. * Get the array of associations to generate.
  153. *
  154. * @param \Cake\ORM\Table $table
  155. * @return array
  156. */
  157. public function getAssociations(Table $table) {
  158. if (!empty($this->params['no-associations'])) {
  159. return [];
  160. }
  161. $this->out(__d('cake_console', 'One moment while associations are detected.'));
  162. $this->listAll();
  163. $associations = [
  164. 'belongsTo' => [],
  165. 'hasMany' => [],
  166. 'belongsToMany' => []
  167. ];
  168. $primary = $table->primaryKey();
  169. if (is_array($primary) && count($primary) > 1) {
  170. $this->err(__d(
  171. 'cake_console',
  172. '<warning>Bake cannot generate associations for composite primary keys at this time</warning>.'
  173. ));
  174. return $associations;
  175. }
  176. $associations = $this->findBelongsTo($table, $associations);
  177. $associations = $this->findHasMany($table, $associations);
  178. $associations = $this->findBelongsToMany($table, $associations);
  179. return $associations;
  180. }
  181. /**
  182. * Find belongsTo relations and add them to the associations list.
  183. *
  184. * @param \Cake\ORM\Table $model Database\Table instance of table being generated.
  185. * @param array $associations Array of in progress associations
  186. * @return array Associations with belongsTo added in.
  187. */
  188. public function findBelongsTo($model, $associations) {
  189. $schema = $model->schema();
  190. $primary = (array)$schema->primaryKey();
  191. foreach ($schema->columns() as $fieldName) {
  192. $offset = strpos($fieldName, '_id');
  193. if (!in_array($fieldName, $primary) && $fieldName !== 'parent_id' && $offset !== false) {
  194. $tmpModelName = $this->_modelNameFromKey($fieldName);
  195. $associations['belongsTo'][] = [
  196. 'alias' => $tmpModelName,
  197. 'foreignKey' => $fieldName,
  198. ];
  199. } elseif ($fieldName === 'parent_id') {
  200. $associations['belongsTo'][] = [
  201. 'alias' => 'Parent' . $model->alias(),
  202. 'foreignKey' => $fieldName,
  203. ];
  204. }
  205. }
  206. return $associations;
  207. }
  208. /**
  209. * Find the hasMany relations and add them to associations list
  210. *
  211. * @param \Cake\ORM\Table $model Model instance being generated
  212. * @param array $associations Array of in progress associations
  213. * @return array Associations with hasMany added in.
  214. */
  215. public function findHasMany($model, $associations) {
  216. $schema = $model->schema();
  217. $primaryKey = (array)$schema->primaryKey();
  218. $tableName = $schema->name();
  219. $foreignKey = $this->_modelKey($tableName);
  220. foreach ($this->listAll() as $otherTable) {
  221. $otherModel = $this->getTableObject($this->_modelName($otherTable), $otherTable);
  222. $otherSchema = $otherModel->schema();
  223. // Exclude habtm join tables.
  224. $pattern = '/_' . preg_quote($tableName, '/') . '|' . preg_quote($tableName, '/') . '_/';
  225. $possibleJoinTable = preg_match($pattern, $otherTable);
  226. if ($possibleJoinTable) {
  227. continue;
  228. }
  229. foreach ($otherSchema->columns() as $fieldName) {
  230. $assoc = false;
  231. if (!in_array($fieldName, $primaryKey) && $fieldName == $foreignKey) {
  232. $assoc = [
  233. 'alias' => $otherModel->alias(),
  234. 'foreignKey' => $fieldName
  235. ];
  236. } elseif ($otherTable == $tableName && $fieldName === 'parent_id') {
  237. $assoc = [
  238. 'alias' => 'Child' . $model->alias(),
  239. 'foreignKey' => $fieldName
  240. ];
  241. }
  242. if ($assoc) {
  243. $associations['hasMany'][] = $assoc;
  244. }
  245. }
  246. }
  247. return $associations;
  248. }
  249. /**
  250. * Find the BelongsToMany relations and add them to associations list
  251. *
  252. * @param \Cake\ORM\Table $model Model instance being generated
  253. * @param array $associations Array of in-progress associations
  254. * @return array Associations with belongsToMany added in.
  255. */
  256. public function findBelongsToMany($model, $associations) {
  257. $schema = $model->schema();
  258. $primaryKey = (array)$schema->primaryKey();
  259. $tableName = $schema->name();
  260. $foreignKey = $this->_modelKey($tableName);
  261. $tables = $this->listAll();
  262. foreach ($tables as $otherTable) {
  263. $assocTable = null;
  264. $offset = strpos($otherTable, $tableName . '_');
  265. $otherOffset = strpos($otherTable, '_' . $tableName);
  266. if ($offset !== false) {
  267. $assocTable = substr($otherTable, strlen($tableName . '_'));
  268. } elseif ($otherOffset !== false) {
  269. $assocTable = substr($otherTable, 0, $otherOffset);
  270. }
  271. if ($assocTable && in_array($assocTable, $tables)) {
  272. $habtmName = $this->_modelName($assocTable);
  273. $associations['belongsToMany'][] = [
  274. 'alias' => $habtmName,
  275. 'foreignKey' => $foreignKey,
  276. 'targetForeignKey' => $this->_modelKey($habtmName),
  277. 'joinTable' => $otherTable
  278. ];
  279. }
  280. }
  281. return $associations;
  282. }
  283. /**
  284. * Get the display field from the model or parameters
  285. *
  286. * @param \Cake\ORM\Table $model The model to introspect.
  287. * @return string
  288. */
  289. public function getDisplayField($model) {
  290. if (!empty($this->params['display-field'])) {
  291. return $this->params['display-field'];
  292. }
  293. return $model->displayField();
  294. }
  295. /**
  296. * Get the primary key field from the model or parameters
  297. *
  298. * @param Cake\ORM\Table $model The model to introspect.
  299. * @return array The columns in the primary key
  300. */
  301. public function getPrimaryKey($model) {
  302. if (!empty($this->params['primary-key'])) {
  303. $fields = explode(',', $this->params['primary-key']);
  304. return array_values(array_filter(array_map('trim', $fields)));
  305. }
  306. return (array)$model->primaryKey();
  307. }
  308. /**
  309. * Get the fields from a model.
  310. *
  311. * Uses the fields and no-fields options.
  312. *
  313. * @param Cake\ORM\Table $model The model to introspect.
  314. * @return array The columns to make accessible
  315. */
  316. public function getFields($model) {
  317. if (!empty($this->params['no-fields'])) {
  318. return [];
  319. }
  320. if (!empty($this->params['fields'])) {
  321. $fields = explode(',', $this->params['fields']);
  322. return array_values(array_filter(array_map('trim', $fields)));
  323. }
  324. $schema = $model->schema();
  325. $columns = $schema->columns();
  326. $primary = $this->getPrimaryKey($model);
  327. $exclude = array_merge($primary, ['created', 'modified', 'updated']);
  328. return array_values(array_diff($columns, $exclude));
  329. }
  330. /**
  331. * Get the hidden fields from a model.
  332. *
  333. * Uses the hidden and no-hidden options.
  334. *
  335. * @param Cake\ORM\Table $model The model to introspect.
  336. * @return array The columns to make accessible
  337. */
  338. public function getHiddenFields($model) {
  339. if (!empty($this->params['no-hidden'])) {
  340. return [];
  341. }
  342. if (!empty($this->params['hidden'])) {
  343. $fields = explode(',', $this->params['hidden']);
  344. return array_values(array_filter(array_map('trim', $fields)));
  345. }
  346. $schema = $model->schema();
  347. $columns = $schema->columns();
  348. $whitelist = ['token', 'password', 'passwd'];
  349. return array_values(array_intersect($columns, $whitelist));
  350. }
  351. /**
  352. * Generate default validation rules.
  353. *
  354. * @param Cake\ORM\Table $model The model to introspect.
  355. * @return array The validation rules.
  356. */
  357. public function getValidation($model) {
  358. if (!empty($this->params['no-validation'])) {
  359. return [];
  360. }
  361. $schema = $model->schema();
  362. $fields = $schema->columns();
  363. if (empty($fields)) {
  364. return false;
  365. }
  366. $skipFields = false;
  367. $validate = [];
  368. $primaryKey = (array)$schema->primaryKey();
  369. foreach ($fields as $fieldName) {
  370. $field = $schema->column($fieldName);
  371. $validation = $this->fieldValidation($fieldName, $field, $primaryKey);
  372. if (!empty($validation)) {
  373. $validate[$fieldName] = $validation;
  374. }
  375. }
  376. return $validate;
  377. }
  378. /**
  379. * Does individual field validation handling.
  380. *
  381. * @param string $fieldName Name of field to be validated.
  382. * @param array $metaData metadata for field
  383. * @param string $primaryKey
  384. * @return array Array of validation for the field.
  385. */
  386. public function fieldValidation($fieldName, $metaData, $primaryKey) {
  387. $ignoreFields = array_merge($primaryKey, ['created', 'modified', 'updated']);
  388. if ($metaData['null'] == true && in_array($fieldName, $ignoreFields)) {
  389. return false;
  390. }
  391. $rule = false;
  392. if ($fieldName === 'email') {
  393. $rule = 'email';
  394. } elseif ($metaData['type'] === 'uuid') {
  395. $rule = 'uuid';
  396. } elseif ($metaData['type'] === 'integer') {
  397. $rule = 'numeric';
  398. } elseif ($metaData['type'] === 'float') {
  399. $rule = 'numeric';
  400. } elseif ($metaData['type'] === 'decimal') {
  401. $rule = 'decimal';
  402. } elseif ($metaData['type'] === 'boolean') {
  403. $rule = 'boolean';
  404. } elseif ($metaData['type'] === 'date') {
  405. $rule = 'date';
  406. } elseif ($metaData['type'] === 'time') {
  407. $rule = 'time';
  408. } elseif ($metaData['type'] === 'datetime') {
  409. $rule = 'datetime';
  410. } elseif ($metaData['type'] === 'inet') {
  411. $rule = 'ip';
  412. }
  413. $allowEmpty = false;
  414. if ($metaData['null'] === true) {
  415. $allowEmpty = true;
  416. }
  417. return [
  418. 'rule' => $rule,
  419. 'allowEmpty' => $allowEmpty,
  420. ];
  421. }
  422. /**
  423. * Get behaviors
  424. *
  425. * @param Cake\ORM\Table $model
  426. * @return array Behaviors
  427. */
  428. public function getBehaviors($model) {
  429. $behaviors = [];
  430. $schema = $model->schema();
  431. $fields = $schema->columns();
  432. if (empty($fields)) {
  433. return [];
  434. }
  435. if (in_array('created', $fields) || in_array('modified', $fields)) {
  436. $behaviors[] = 'Timestamp';
  437. }
  438. if (in_array('lft', $fields) && $schema->columnType('lft') === 'integer' &&
  439. in_array('rght', $fields) && $schema->columnType('rght') === 'integer' &&
  440. in_array('parent_id', $fields)
  441. ) {
  442. $behaviors[] = 'Tree';
  443. }
  444. return $behaviors;
  445. }
  446. /**
  447. * Bake an entity class.
  448. *
  449. * @param \Cake\ORM\Table $model Model name or object
  450. * @param array $data An array to use to generate the Table
  451. * @return string
  452. */
  453. public function bakeEntity($model, $data = []) {
  454. if (!empty($this->params['no-entity'])) {
  455. return;
  456. }
  457. $name = Inflector::singularize($model->alias());
  458. $ns = Configure::read('App.namespace');
  459. $pluginPath = '';
  460. if ($this->plugin) {
  461. $ns = $this->plugin;
  462. $pluginPath = $this->plugin . '.';
  463. }
  464. $data += [
  465. 'name' => $name,
  466. 'namespace' => $ns,
  467. 'plugin' => $this->plugin,
  468. 'pluginPath' => $pluginPath,
  469. 'fields' => [],
  470. ];
  471. $this->Template->set($data);
  472. $out = $this->Template->generate('classes', 'entity');
  473. $path = $this->getPath();
  474. $filename = $path . 'Entity/' . $name . '.php';
  475. $this->out("\n" . __d('cake_console', 'Baking entity class for %s...', $name), 1, Shell::QUIET);
  476. $this->createFile($filename, $out);
  477. return $out;
  478. }
  479. /**
  480. * Bake a table class.
  481. *
  482. * @param \Cake\ORM\Table $model Model name or object
  483. * @param array $data An array to use to generate the Table
  484. * @return string
  485. */
  486. public function bakeTable($model, $data = []) {
  487. if (!empty($this->params['no-table'])) {
  488. return;
  489. }
  490. $ns = Configure::read('App.namespace');
  491. $pluginPath = '';
  492. if ($this->plugin) {
  493. $ns = $this->plugin;
  494. $pluginPath = $this->plugin . '.';
  495. }
  496. $name = $model->alias();
  497. $data += [
  498. 'plugin' => $this->plugin,
  499. 'pluginPath' => $pluginPath,
  500. 'namespace' => $ns,
  501. 'name' => $name,
  502. 'associations' => [],
  503. 'primaryKey' => 'id',
  504. 'displayField' => null,
  505. 'table' => null,
  506. 'validation' => [],
  507. 'behaviors' => [],
  508. ];
  509. $this->Template->set($data);
  510. $out = $this->Template->generate('classes', 'table');
  511. $path = $this->getPath();
  512. $filename = $path . 'Table/' . $name . 'Table.php';
  513. $this->out("\n" . __d('cake_console', 'Baking table class for %s...', $name), 1, Shell::QUIET);
  514. $this->createFile($filename, $out);
  515. TableRegistry::clear();
  516. return $out;
  517. }
  518. /**
  519. * Outputs the a list of possible models or controllers from database
  520. *
  521. * @return array
  522. */
  523. public function listAll() {
  524. if (!empty($this->_tables)) {
  525. return $this->_tables;
  526. }
  527. $this->_modelNames = [];
  528. $this->_tables = $this->_getAllTables();
  529. foreach ($this->_tables as $table) {
  530. $this->_modelNames[] = $this->_modelName($table);
  531. }
  532. return $this->_tables;
  533. }
  534. /**
  535. * Get an Array of all the tables in the supplied connection
  536. * will halt the script if no tables are found.
  537. *
  538. * @return array Array of tables in the database.
  539. * @throws InvalidArgumentException When connection class
  540. * does not have a schemaCollection method.
  541. */
  542. protected function _getAllTables() {
  543. $tables = [];
  544. $db = ConnectionManager::get($this->connection);
  545. if (!method_exists($db, 'schemaCollection')) {
  546. $this->err(__d(
  547. 'cake_console',
  548. 'Connections need to implement schemaCollection() to be used with bake.'
  549. ));
  550. return $this->_stop();
  551. }
  552. $schema = $db->schemaCollection();
  553. $tables = $schema->listTables();
  554. if (empty($tables)) {
  555. $this->err(__d('cake_console', 'Your database does not have any tables.'));
  556. return $this->_stop();
  557. }
  558. sort($tables);
  559. return $tables;
  560. }
  561. /**
  562. * Get the table name for the model being baked.
  563. *
  564. * Uses the `table` option if it is set.
  565. *
  566. * @param string $name Table name
  567. * @return string
  568. */
  569. public function getTable($name) {
  570. if (isset($this->params['table'])) {
  571. return $this->params['table'];
  572. }
  573. return Inflector::tableize($name);
  574. }
  575. /**
  576. * Gets the option parser instance and configures it.
  577. *
  578. * @return ConsoleOptionParser
  579. */
  580. public function getOptionParser() {
  581. $parser = parent::getOptionParser();
  582. $parser->description(
  583. __d('cake_console', 'Bake models.')
  584. )->addArgument('name', [
  585. 'help' => __d('cake_console', 'Name of the model to bake. Can use Plugin.name to bake plugin models.')
  586. ])->addSubcommand('all', [
  587. 'help' => __d('cake_console', 'Bake all model files with associations and validation.')
  588. ])->addOption('plugin', [
  589. 'short' => 'p',
  590. 'help' => __d('cake_console', 'Plugin to bake the model into.')
  591. ])->addOption('theme', [
  592. 'short' => 't',
  593. 'help' => __d('cake_console', 'Theme to use when baking code.')
  594. ])->addOption('connection', [
  595. 'short' => 'c',
  596. 'help' => __d('cake_console', 'The connection the model table is on.')
  597. ])->addOption('force', [
  598. 'short' => 'f',
  599. 'help' => __d('cake_console', 'Force overwriting existing files without prompting.')
  600. ])->addOption('table', [
  601. 'help' => __d('cake_console', 'The table name to use if you have non-conventional table names.')
  602. ])->addOption('no-entity', [
  603. 'boolean' => true,
  604. 'help' => __d('cake_console', 'Disable generating an entity class.')
  605. ])->addOption('no-table', [
  606. 'boolean' => true,
  607. 'help' => __d('cake_console', 'Disable generating a table class.')
  608. ])->addOption('no-validation', [
  609. 'boolean' => true,
  610. 'help' => __d('cake_console', 'Disable generating validation rules.')
  611. ])->addOption('no-associations', [
  612. 'boolean' => true,
  613. 'help' => __d('cake_console', 'Disable generating associations.')
  614. ])->addOption('no-fields', [
  615. 'boolean' => true,
  616. 'help' => __d('cake_console', 'Disable generating accessible fields in the entity.')
  617. ])->addOption('fields', [
  618. 'help' => __d('cake_console', 'A comma separated list of fields to make accessible.')
  619. ])->addOption('no-hidden', [
  620. 'boolean' => true,
  621. 'help' => __d('cake_console', 'Disable generating hidden fields in the entity.')
  622. ])->addOption('hidden', [
  623. 'help' => __d('cake_console', 'A comma separated list of fields to hide.')
  624. ])->addOption('primary-key', [
  625. 'help' => __d('cake_console', 'The primary key if you would like to manually set one. Can be a comma separated list if you are using a composite primary key.')
  626. ])->addOption('display-field', [
  627. 'help' => __d('cake_console', 'The displayField if you would like to choose one.')
  628. ])->addOption('no-test', [
  629. 'boolean' => true,
  630. 'help' => __d('cake_console', 'Do not generate a test case skeleton.')
  631. ])->addOption('no-fixture', [
  632. 'boolean' => true,
  633. 'help' => __d('cake_console', 'Do not generate a test fixture skeleton.')
  634. ])->epilog(
  635. __d('cake_console', 'Omitting all arguments and options will list ' .
  636. 'the table names you can generate models for')
  637. );
  638. return $parser;
  639. }
  640. /**
  641. * Interact with FixtureTask to automatically bake fixtures when baking models.
  642. *
  643. * @param string $className Name of class to bake fixture for
  644. * @param string $useTable Optional table name for fixture to use.
  645. * @return void
  646. * @see FixtureTask::bake
  647. */
  648. public function bakeFixture($className, $useTable = null) {
  649. if (!empty($this->params['no-fixture'])) {
  650. return;
  651. }
  652. $this->Fixture->connection = $this->connection;
  653. $this->Fixture->plugin = $this->plugin;
  654. $this->Fixture->bake($className, $useTable);
  655. }
  656. /**
  657. * Assembles and writes a unit test file
  658. *
  659. * @param string $className Model class name
  660. * @return string
  661. */
  662. public function bakeTest($className) {
  663. if (!empty($this->params['no-test'])) {
  664. return;
  665. }
  666. $this->Test->plugin = $this->plugin;
  667. $this->Test->connection = $this->connection;
  668. return $this->Test->bake('Model', $className);
  669. }
  670. }