/protected/extensions/gii-template-collection-develop/fullModel/templates/default/basemodel.php

https://github.com/mithereal/yii-runends-template · PHP · 241 lines · 172 code · 12 blank · 57 comment · 9 complexity · c5af03465be66172a81e48d213232624 MD5 · raw file

  1. <?php
  2. /**
  3. * This is the template for generating the model class of a specified table.
  4. * In addition to the default model Code, this adds the GtcSaveRelationsBehavior
  5. * to the model class definition.
  6. * - $this: the ModelCode object
  7. * - $tableName: the table name for this class (prefix is already removed if necessary)
  8. * - $modelClass: the model class name
  9. * - $columns: list of table columns (name=>CDbColumnSchema)
  10. * - $labels: list of attribute labels (name=>label)
  11. * - $rules: list of validation rules
  12. * - $relations: list of relations (name=>relation declaration)
  13. */
  14. ?>
  15. <?php echo "<?php\n"; ?>
  16. /**
  17. * This is the model base class for the table "<?php echo $tableName; ?>".
  18. *
  19. * Columns in table "<?php echo $tableName; ?>" available as properties of the model:
  20. <?php foreach($columns as $column): ?>
  21. * @property <?php echo $column->type.' $'.$column->name."\n"; ?>
  22. <?php endforeach; ?>
  23. *
  24. <?php if(count($relations)>0): ?>
  25. * Relations of table "<?php echo $tableName; ?>" available as properties of the model:
  26. <?php else: ?>
  27. * There are no model relations.
  28. <?php endif; ?>
  29. <?php foreach($relations as $name=>$relation): ?>
  30. * @property <?php
  31. if (preg_match("~^array\(self::([^,]+), '([^']+)', '([^']+)'\)$~", $relation, $matches))
  32. {
  33. $relationType = $matches[1];
  34. $relationModel = $matches[2];
  35. switch($relationType){
  36. case 'HAS_ONE':
  37. echo $relationModel.' $'.$name."\n";
  38. break;
  39. case 'BELONGS_TO':
  40. echo $relationModel.' $'.$name."\n";
  41. break;
  42. case 'HAS_MANY':
  43. echo $relationModel.'[] $'.$name."\n";
  44. break;
  45. case 'MANY_MANY':
  46. echo $relationModel.'[] $'.$name."\n";
  47. break;
  48. default:
  49. echo 'mixed $'.$name."\n";
  50. }
  51. }
  52. ?>
  53. <?php endforeach; ?>
  54. */
  55. abstract class <?php echo 'Base' . $modelClass; ?> extends <?php echo $this->baseClass."\n"; ?>
  56. {
  57. <?php
  58. if(!empty($enum)){
  59. ?>
  60. /**
  61. * ENUM field values
  62. */
  63. <?php
  64. foreach($enum as $column_name => $enum_values){
  65. foreach ($enum_values as $enum_value){
  66. echo ' const ' . $enum_value['const_name'] . ' = \'' . $enum_value['value'] . '\';' . PHP_EOL;
  67. }
  68. }
  69. }
  70. ?>
  71. public static function model($className = __CLASS__)
  72. {
  73. return parent::model($className);
  74. }
  75. public function tableName()
  76. {
  77. return '<?php echo $tableName; ?>';
  78. }
  79. public function rules()
  80. {
  81. return array_merge(
  82. parent::rules(), array(
  83. <?php
  84. foreach($rules as $rule) {
  85. echo " $rule,\n";
  86. }
  87. ?>
  88. array('<?php echo implode(', ', array_keys($columns)); ?>', 'safe', 'on' => 'search'),
  89. )
  90. );
  91. }
  92. public function getItemLabel()
  93. {
  94. return (string) $this-><?php
  95. $found = false;
  96. foreach($columns as $name => $column) {
  97. if(!$found
  98. && $column->type != 'datetime'
  99. && $column->type==='string'
  100. && !$column->isPrimaryKey) {
  101. echo $column->name;
  102. $found = true;
  103. }
  104. }
  105. // if the columns contains no column of type 'string', return the
  106. // first column (usually the primary key)
  107. if(!$found)
  108. echo reset($columns)->name;
  109. ?>;
  110. }
  111. public function behaviors()
  112. {
  113. return array_merge(
  114. parent::behaviors(), array(
  115. 'savedRelated' => array(
  116. 'class' => '\GtcSaveRelationsBehavior'
  117. )
  118. )
  119. );
  120. }
  121. public function relations()
  122. {
  123. return array_merge(
  124. parent::relations(), array(
  125. <?php
  126. foreach($relations as $name=>$relation) {
  127. echo " '$name' => $relation,\n";
  128. }
  129. ?>
  130. )
  131. );
  132. }
  133. public function attributeLabels()
  134. {
  135. return array(
  136. <?php
  137. foreach($labels as $name=>$label) {
  138. echo " '$name' => Yii::t('".$this->messageCatalog."', '$label'),\n";
  139. }
  140. ?>
  141. );
  142. }
  143. <?php
  144. $aEnumLabels = array();
  145. foreach ($columns as $column) {
  146. if (substr(strtoupper($column->dbType), 0, 4) == 'ENUM') {
  147. $enum_values = explode(',', substr($column->dbType, 4, strlen($column->dbType) - 1));
  148. $aEnumLabels[$column->name] = array();
  149. foreach ($enum_values as $value) {
  150. $value = trim($value, "()'");
  151. $label = ucwords(trim(strtolower(str_replace(array('-', '_'), ' ', preg_replace('/(?<![A-Z])[A-Z]/', ' \0', $value)))));
  152. $label = preg_replace('/\s+/', ' ', $label);
  153. $aEnumLabels[$column->name][$value] = $label;
  154. }
  155. }
  156. }
  157. if(!empty($enum)){
  158. ?>
  159. public function enumLabels()
  160. {
  161. return array(
  162. <?php
  163. foreach($enum as $column_name => $enum_values){
  164. echo " '$column_name' => array(" . PHP_EOL;
  165. foreach ($enum_values as $enum_value){
  166. echo " self::{$enum_value['const_name']} => Yii::t('" . $this->messageCatalog . "', '{$enum_value['const_name']}')," . PHP_EOL;
  167. }
  168. echo " )," . PHP_EOL;
  169. }
  170. ?>
  171. );
  172. }
  173. public function getEnumFieldLabels($column){
  174. $aLabels = $this->enumLabels();
  175. return $aLabels[$column];
  176. }
  177. public function getEnumLabel($column,$value){
  178. $aLabels = $this->enumLabels();
  179. if(!isset($aLabels[$column])){
  180. return $value;
  181. }
  182. if(!isset($aLabels[$column][$value])){
  183. return $value;
  184. }
  185. return $aLabels[$column][$value];
  186. }
  187. <?php
  188. }
  189. ?>
  190. public function searchCriteria($criteria = null)
  191. {
  192. if (is_null($criteria)) {
  193. $criteria = new CDbCriteria;
  194. }
  195. <?php
  196. foreach($columns as $name=>$column)
  197. {
  198. if($column->type==='string' and !$column->isForeignKey)
  199. {
  200. echo " \$criteria->compare('t.$name', \$this->$name, true);\n";
  201. }
  202. else
  203. {
  204. echo " \$criteria->compare('t.$name', \$this->$name);\n";
  205. }
  206. }
  207. echo "\n";
  208. ?>
  209. return $criteria;
  210. }
  211. }