我想在CActiveRecord中添加改进的方法,但是如果class Project_Model extends CActiveRecord {}出错
在数据库中找不到活动记录类"Project_ActiveRecord“的表"Project_ActiveRecord”。
我希望创建简单的结构:CActiveRecord->Project(只扩展方法)->Table(实表)。
怎么能做到这一点?
发布于 2013-04-18 08:52:31
错误很明显:数据库中没有一个名为Project_ActiveRecord的表!
如果Project_Model将成为其他活动记录模型的基本模型,那么您应该执行如下操作:
//A base classe example that has a behavior shared by all the inherited class
abstract class Project_Model extends CActiveRecord
{
public function behaviors(){
return array(
'CTimestampBehavior' => array(
'class' => 'zii.behaviors.CTimestampBehavior',
'setUpdateOnCreate' => true
),
);
}
}然后,您可以声明另一个类,即将在db中有一个表。
class YourClass extends Project_Model
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return Token the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'YourClassTable';
}
...
}那么您应该永远不要在代码中调用类Project_Model (这就是为什么我将关键字abstract),您必须调用继承的类,其中在db中有一个现有的表!
https://stackoverflow.com/questions/16077927
复制相似问题