ModelTask.php 22 KB

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