model.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * This is the template for generating the model class of a specified table.
  4. * - $this: the ModelCode object
  5. * - $tableName: the table name for this class (prefix is already removed if necessary)
  6. * - $modelClass: the model class name
  7. * - $columns: list of table columns (name=>CDbColumnSchema)
  8. * - $labels: list of attribute labels (name=>label)
  9. * - $rules: list of validation rules
  10. * - $relations: list of relations (name=>relation declaration)
  11. */
  12. ?>
  13. <?php echo "<?php\n"; ?>
  14. /**
  15. * This is the model class for table "<?php echo $tableName; ?>".
  16. *
  17. * The followings are the available columns in table '<?php echo $tableName; ?>':
  18. <?php foreach($columns as $column): ?>
  19. * @property <?php echo $column->type.' $'.$column->name."\n"; ?>
  20. <?php endforeach; ?>
  21. <?php if(!empty($relations)): ?>
  22. *
  23. * The followings are the available model relations:
  24. <?php foreach($relations as $name=>$relation): ?>
  25. * @property <?php
  26. if (preg_match("~^array\(self::([^,]+), '([^']+)', '([^']+)'\)$~", $relation, $matches))
  27. {
  28. $relationType = $matches[1];
  29. $relationModel = $matches[2];
  30. switch($relationType){
  31. case 'HAS_ONE':
  32. echo $relationModel.' $'.$name."\n";
  33. break;
  34. case 'BELONGS_TO':
  35. echo $relationModel.' $'.$name."\n";
  36. break;
  37. case 'HAS_MANY':
  38. echo $relationModel.'[] $'.$name."\n";
  39. break;
  40. case 'MANY_MANY':
  41. echo $relationModel.'[] $'.$name."\n";
  42. break;
  43. default:
  44. echo 'mixed $'.$name."\n";
  45. }
  46. }
  47. ?>
  48. <?php endforeach; ?>
  49. <?php endif; ?>
  50. */
  51. class <?php echo $modelClass; ?> extends <?php echo $this->baseClass."\n"; ?>
  52. {
  53. /**
  54. * @return string the associated database table name
  55. */
  56. public function tableName()
  57. {
  58. return '<?php echo $tableName; ?>';
  59. }
  60. /**
  61. * @return array validation rules for model attributes.
  62. */
  63. public function rules()
  64. {
  65. // NOTE: you should only define rules for those attributes that
  66. // will receive user inputs.
  67. return array(
  68. <?php foreach($rules as $rule): ?>
  69. <?php echo $rule.",\n"; ?>
  70. <?php endforeach; ?>
  71. // The following rule is used by search().
  72. // @todo Please remove those attributes that should not be searched.
  73. array('<?php echo implode(', ', array_keys($columns)); ?>', 'safe', 'on'=>'search'),
  74. );
  75. }
  76. /**
  77. * @return array relational rules.
  78. */
  79. public function relations()
  80. {
  81. // NOTE: you may need to adjust the relation name and the related
  82. // class name for the relations automatically generated below.
  83. return array(
  84. <?php foreach($relations as $name=>$relation): ?>
  85. <?php echo "'$name' => $relation,\n"; ?>
  86. <?php endforeach; ?>
  87. );
  88. }
  89. /**
  90. * @return array customized attribute labels (name=>label)
  91. */
  92. public function attributeLabels()
  93. {
  94. return array(
  95. <?php foreach($labels as $name=>$label): ?>
  96. <?php echo "'".$name."' => '".str_replace("'","\'",$label)."',\n"; ?>
  97. <?php endforeach; ?>
  98. );
  99. }
  100. /**
  101. * Retrieves a list of models based on the current search/filter conditions.
  102. *
  103. * Typical usecase:
  104. * - Initialize the model fields with values from filter form.
  105. * - Execute this method to get CActiveDataProvider instance which will filter
  106. * models according to data in model fields.
  107. * - Pass data provider to CGridView, CListView or any similar widget.
  108. *
  109. * @return CActiveDataProvider the data provider that can return the models
  110. * based on the search/filter conditions.
  111. */
  112. public function search()
  113. {
  114. // @todo Please modify the following code to remove attributes that should not be searched.
  115. $criteria=new CDbCriteria;
  116. <?php
  117. foreach($columns as $name=>$column)
  118. {
  119. if($column->type==='string')
  120. {
  121. echo "\t\t\$criteria->compare('$name',\$this->$name,true);\n";
  122. }
  123. else
  124. {
  125. echo "\t\t\$criteria->compare('$name',\$this->$name);\n";
  126. }
  127. }
  128. ?>
  129. return new CActiveDataProvider($this, array(
  130. 'criteria'=>$criteria,
  131. ));
  132. }
  133. <?php if($connectionId!='db'):?>
  134. /**
  135. * @return CDbConnection the database connection used for this class
  136. */
  137. public function getDbConnection()
  138. {
  139. return Yii::app()-><?php echo $connectionId ?>;
  140. }
  141. <?php endif?>
  142. /**
  143. * Returns the static model of the specified AR class.
  144. * Please note that you should have this exact method in all your CActiveRecord descendants!
  145. * @param string $className active record class name.
  146. * @return <?php echo $modelClass; ?> the static model class
  147. */
  148. public static function model($className=__CLASS__)
  149. {
  150. return parent::model($className);
  151. }
  152. }