Yii 虚拟属性与模型

Yii 虚拟属性与模型,yii,model,virtual-attribute,Yii,Model,Virtual Attribute,虚拟属性能否表示模型?我读过这篇文章,但没有找到答案 编辑: 目的是让我的模型对用户更明确 abstract class BaseDonnee{ protected $info; public function representingColumn(){ return 'info'; //Please, I'm not sure this is right } public function setInfo(){ //I can

虚拟属性能否表示模型?我读过这篇文章,但没有找到答案

编辑: 目的是让我的模型对用户更明确

abstract class BaseDonnee{
    protected $info;

    public function representingColumn(){
        return 'info'; //Please, I'm not sure this is right
    }

    public function setInfo(){
        //I can set the attribute's value
        $this->info = Info::model()->find('a condition')->info;
    }

    public function getInfo(){
        return $this->info;
    }
}
编辑2: 运行代码时,我可以得到一个异常:

CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: [Microsoft][SQL Server Native Client 11.0][SQL Server]Nom de colonne non valide : 'info'..

我如何继续它的工作?

是的,但数据提供者等不会自动填写它

class User
{

    /**
     * @var Address
     */
    private $_address = null;

    public function getAddress()
    {
        return $this->_address;
    }

    public function setAddress(Address $address)
    {
        $this->_address = $address;
    }

}
然后您可以使用它:

$user = new User;

$user->address = new Address();

var_dump($user->address->street); // Assuming class Address has street field

我看得出来。但在创建类似的代码时,如果没有找到我的属性,我可能会遇到一个异常:CDbCommand无法执行SQL语句:SQLSTATE[42S22]:[Microsoft][SQL Server Native Client 11.0][SQL Server]Nom de colonne non-valide:'info'。。我想我需要用我的模板更新我的问题。