创建一个CActiveRecord模型
public function tableName(){
return __CLASS__;
}
public static function model($className = __CLASS__){
return parent::model($className);
}
class table extends CActiveRecord{ //table为表名,为了使用方便
}
使用时可以直接使用
table::model()->find() ;
或者
$model = new table();
$model->find();
但其实两者是有所区别的,前者使用静态方法创建类实例 与 类中所使用的$this为相同,单一操作不会有影响,当有查询后的添加操作时,将导致无法新增数据,log里看到程序是进行的update操作。
而后者,new一个新对象,与类中$this对象是不冲突的
所以建议:
模型中既有查询又有插入操作时使用 new 方法创建对象进行操作
只在使用单纯的查询时使用
table::model()方法