0

Can a virtual attribute represent a model ? I've read this wiki but don't find answer.

Edit : The aim is to make my model more explicit for the user.

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;
    }
}

Edit 2 : When running the code, I can get an exception :

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

How can I proceed to it work ?

sk001
  • 561
  • 6
  • 27

1 Answers1

0

Yes, but it will not be automatically filled by dataprovider etc.

class User
{

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

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

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

}

Then you can use it:

$user = new User;

$user->address = new Address();

var_dump($user->address->street); // Assuming class Address has street field
  • I can see. But when creating a similar code, I can face an exception assuming that my attribute is not found : `CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: [Microsoft][SQL Server Native Client 11.0][SQL Server]Nom de colonne non valide : 'info'..` I think I need to update my question with my template. – sk001 Oct 15 '14 at 14:06