Add Extra Attribute In Model

Hi Everyone

I want to add extra attribute in Model from another table

but I don’t want to generate a related Model AR for the last one table

for example

If I have the AR Model for table related_table I could use

 public function relations() {

        return array(

            'product' => array(self::BELONGS_TO, 'relatedTableAR', 'relId')



How could achieve the similar but directly to the table (without relatedTableAR)

like this

 public function relations() {

        return array(

            //'product' => array(self::BELONGS_TO, 'relatedTableAR', 'relId')

 'Products' => array(self::BELONGS_TO, ':related_table', '' , 'on'=>'','select'=>'id,price'), 




I suppose this code should help you. Yes: you can add a method to a module that get another table instance

class MyModelTwo {

    public $tableTwoAttribute;


class MyModelOne {

    public function getOtherTable()


        return MyModelTwo::model()->findByPk($this->model_two_id);



And use this "getter" to load ather tables attributes.

$modelOne = MyModelOne::model()->findByPk();


It’s fast to code.

Hi sensorario and thanks for your response

I suppose this way requires the MyModelTwo requires to extends CActiveRecord because you used


So I have to make a model! So can’t be without AR

Another Solution I thought is by executeSql method but I would like to avoid it in this issue…

Hi KonApaz

I have done this in past I dont know if this fit your needs

class Model extends CActiveRecord {

  public $propteryFromSecondModel;


$criteria = new CDbCriteria;

$criteria->select= "`t`.`*`, (SELECT `t2`.`fieldname` FROM `table2` `t2` WHERE YOURCONDITION) as `propertyFromSecondModel`";


basically declare a public property/attribute in your model and Yii will take of the rest.