model.ctp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * Model template file.
  4. *
  5. * Used by bake to create new Model files.
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://cakephp.org CakePHP(tm) Project
  17. * @package Cake.Console.Templates.default.classes
  18. * @since CakePHP(tm) v 1.3
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. echo "<?php\n";
  22. echo "App::uses('{$plugin}AppModel', '{$pluginPath}Model');\n";
  23. ?>
  24. /**
  25. * <?php echo $name ?> Model
  26. *
  27. <?php
  28. foreach (array('hasOne', 'belongsTo', 'hasMany', 'hasAndBelongsToMany') as $assocType) {
  29. if (!empty($associations[$assocType])) {
  30. foreach ($associations[$assocType] as $relation) {
  31. echo " * @property {$relation['className']} \${$relation['alias']}\n";
  32. }
  33. }
  34. }
  35. ?>
  36. */
  37. class <?php echo $name ?> extends <?php echo $plugin; ?>AppModel {
  38. <?php if ($useDbConfig != 'default'): ?>
  39. /**
  40. * Use database config
  41. *
  42. * @var string
  43. */
  44. public $useDbConfig = '<?php echo $useDbConfig; ?>';
  45. <?php endif;
  46. if ($useTable && $useTable !== Inflector::tableize($name)):
  47. $table = "'$useTable'";
  48. echo "/**\n * Use table\n *\n * @var mixed False or table name\n */\n";
  49. echo "\tpublic \$useTable = $table;\n\n";
  50. endif;
  51. if ($primaryKey !== 'id'): ?>
  52. /**
  53. * Primary key field
  54. *
  55. * @var string
  56. */
  57. public $primaryKey = '<?php echo $primaryKey; ?>';
  58. <?php endif;
  59. if ($displayField): ?>
  60. /**
  61. * Display field
  62. *
  63. * @var string
  64. */
  65. public $displayField = '<?php echo $displayField; ?>';
  66. <?php endif;
  67. if (!empty($validate)):
  68. echo "/**\n * Validation rules\n *\n * @var array\n */\n";
  69. echo "\tpublic \$validate = array(\n";
  70. foreach ($validate as $field => $validations):
  71. echo "\t\t'$field' => array(\n";
  72. foreach ($validations as $key => $validator):
  73. echo "\t\t\t'$key' => array(\n";
  74. echo "\t\t\t\t'rule' => array('$validator'),\n";
  75. echo "\t\t\t\t//'message' => 'Your custom message here',\n";
  76. echo "\t\t\t\t//'allowEmpty' => false,\n";
  77. echo "\t\t\t\t//'required' => false,\n";
  78. echo "\t\t\t\t//'last' => false, // Stop validation after this rule\n";
  79. echo "\t\t\t\t//'on' => 'create', // Limit validation to 'create' or 'update' operations\n";
  80. echo "\t\t\t),\n";
  81. endforeach;
  82. echo "\t\t),\n";
  83. endforeach;
  84. echo "\t);\n";
  85. endif;
  86. foreach ($associations as $assoc):
  87. if (!empty($assoc)):
  88. ?>
  89. //The Associations below have been created with all possible keys, those that are not needed can be removed
  90. <?php
  91. break;
  92. endif;
  93. endforeach;
  94. foreach (array('hasOne', 'belongsTo') as $assocType):
  95. if (!empty($associations[$assocType])):
  96. $typeCount = count($associations[$assocType]);
  97. echo "\n/**\n * $assocType associations\n *\n * @var array\n */";
  98. echo "\n\tpublic \$$assocType = array(";
  99. foreach ($associations[$assocType] as $i => $relation):
  100. $out = "\n\t\t'{$relation['alias']}' => array(\n";
  101. $out .= "\t\t\t'className' => '{$relation['className']}',\n";
  102. $out .= "\t\t\t'foreignKey' => '{$relation['foreignKey']}',\n";
  103. $out .= "\t\t\t'conditions' => '',\n";
  104. $out .= "\t\t\t'fields' => '',\n";
  105. $out .= "\t\t\t'order' => ''\n";
  106. $out .= "\t\t)";
  107. if ($i + 1 < $typeCount) {
  108. $out .= ",";
  109. }
  110. echo $out;
  111. endforeach;
  112. echo "\n\t);\n";
  113. endif;
  114. endforeach;
  115. if (!empty($associations['hasMany'])):
  116. $belongsToCount = count($associations['hasMany']);
  117. echo "\n/**\n * hasMany associations\n *\n * @var array\n */";
  118. echo "\n\tpublic \$hasMany = array(";
  119. foreach ($associations['hasMany'] as $i => $relation):
  120. $out = "\n\t\t'{$relation['alias']}' => array(\n";
  121. $out .= "\t\t\t'className' => '{$relation['className']}',\n";
  122. $out .= "\t\t\t'foreignKey' => '{$relation['foreignKey']}',\n";
  123. $out .= "\t\t\t'dependent' => false,\n";
  124. $out .= "\t\t\t'conditions' => '',\n";
  125. $out .= "\t\t\t'fields' => '',\n";
  126. $out .= "\t\t\t'order' => '',\n";
  127. $out .= "\t\t\t'limit' => '',\n";
  128. $out .= "\t\t\t'offset' => '',\n";
  129. $out .= "\t\t\t'exclusive' => '',\n";
  130. $out .= "\t\t\t'finderQuery' => '',\n";
  131. $out .= "\t\t\t'counterQuery' => ''\n";
  132. $out .= "\t\t)";
  133. if ($i + 1 < $belongsToCount) {
  134. $out .= ",";
  135. }
  136. echo $out;
  137. endforeach;
  138. echo "\n\t);\n\n";
  139. endif;
  140. if (!empty($associations['hasAndBelongsToMany'])):
  141. $habtmCount = count($associations['hasAndBelongsToMany']);
  142. echo "\n/**\n * hasAndBelongsToMany associations\n *\n * @var array\n */";
  143. echo "\n\tpublic \$hasAndBelongsToMany = array(";
  144. foreach ($associations['hasAndBelongsToMany'] as $i => $relation):
  145. $out = "\n\t\t'{$relation['alias']}' => array(\n";
  146. $out .= "\t\t\t'className' => '{$relation['className']}',\n";
  147. $out .= "\t\t\t'joinTable' => '{$relation['joinTable']}',\n";
  148. $out .= "\t\t\t'foreignKey' => '{$relation['foreignKey']}',\n";
  149. $out .= "\t\t\t'associationForeignKey' => '{$relation['associationForeignKey']}',\n";
  150. $out .= "\t\t\t'unique' => 'keepExisting',\n";
  151. $out .= "\t\t\t'conditions' => '',\n";
  152. $out .= "\t\t\t'fields' => '',\n";
  153. $out .= "\t\t\t'order' => '',\n";
  154. $out .= "\t\t\t'limit' => '',\n";
  155. $out .= "\t\t\t'offset' => '',\n";
  156. $out .= "\t\t\t'finderQuery' => '',\n";
  157. $out .= "\t\t\t'deleteQuery' => '',\n";
  158. $out .= "\t\t\t'insertQuery' => ''\n";
  159. $out .= "\t\t)";
  160. if ($i + 1 < $habtmCount) {
  161. $out .= ",";
  162. }
  163. echo $out;
  164. endforeach;
  165. echo "\n\t);\n\n";
  166. endif;
  167. ?>
  168. }