model.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * This is the template for generating a model class file.
  4. * The following variables are available in this template:
  5. * - $className: the class name
  6. * - $tableName: the table name
  7. * - $columns: a list of table column schema objects
  8. * - $rules: a list of validation rules (string)
  9. * - $labels: a list of labels (column name => label)
  10. * - $relations: a list of relations (string)
  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. */
  22. class <?php echo $className; ?> extends CActiveRecord
  23. {
  24. /**
  25. * @return string the associated database table name
  26. */
  27. public function tableName()
  28. {
  29. return '<?php echo $tableName; ?>';
  30. }
  31. /**
  32. * @return array validation rules for model attributes.
  33. */
  34. public function rules()
  35. {
  36. // NOTE: you should only define rules for those attributes that
  37. // will receive user inputs.
  38. return array(
  39. <?php foreach($rules as $rule): ?>
  40. <?php echo $rule.",\n"; ?>
  41. <?php endforeach; ?>
  42. // The following rule is used by search().
  43. // Please remove those attributes that should not be searched.
  44. array('<?php echo implode(', ', array_keys($columns)); ?>', 'safe', 'on'=>'search'),
  45. );
  46. }
  47. /**
  48. * @return array relational rules.
  49. */
  50. public function relations()
  51. {
  52. // NOTE: you may need to adjust the relation name and the related
  53. // class name for the relations automatically generated below.
  54. return array(
  55. <?php foreach($relations as $name=>$relation): ?>
  56. <?php echo "'$name' => $relation,\n"; ?>
  57. <?php endforeach; ?>
  58. );
  59. }
  60. /**
  61. * @return array customized attribute labels (name=>label)
  62. */
  63. public function attributeLabels()
  64. {
  65. return array(
  66. <?php foreach($labels as $column=>$label): ?>
  67. <?php echo "'$column' => '$label',\n"; ?>
  68. <?php endforeach; ?>
  69. );
  70. }
  71. /**
  72. * Retrieves a list of models based on the current search/filter conditions.
  73. *
  74. * Typical usecase:
  75. * - Initialize the model fields with values from filter form.
  76. * - Execute this method to get CActiveDataProvider instance which will filter
  77. * models according to data in model fields.
  78. * - Pass data provider to CGridView, CListView or any similar widget.
  79. *
  80. * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
  81. */
  82. public function search()
  83. {
  84. // Warning: Please modify the following code to remove attributes that
  85. // should not be searched.
  86. $criteria=new CDbCriteria;
  87. <?php
  88. foreach($columns as $name=>$column)
  89. {
  90. if($column->type==='string')
  91. {
  92. echo "\t\t\$criteria->compare('$name',\$this->$name,true);\n\n";
  93. }
  94. else
  95. {
  96. echo "\t\t\$criteria->compare('$name',\$this->$name);\n\n";
  97. }
  98. }
  99. ?>
  100. return new CActiveDataProvider('<?php echo $className; ?>', array(
  101. 'criteria'=>$criteria,
  102. ));
  103. }
  104. /**
  105. * Returns the static model of the specified AR class.
  106. * @return <?php echo $className; ?> the static model class
  107. */
  108. public static function model($className=__CLASS__)
  109. {
  110. return parent::model($className);
  111. }
  112. }