ModelTask.php 20 KB

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