ModelTask.php 20 KB

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