Ar Dont Return From Another Table

Why dont return the b.title?


$limit = 10;

$model = Comment::model()->findAll(array(

    'condition'=>'t.status=2',

    'select'=>'t.id, b.title, t.author_id',

    'order'=>'t.create_time DESC',

    'limit'=>$limit,

    'alias'=>'t',

    'join'=>'left join tbl_blog b on(t.forum_id=b.forum_id)'

));

var_dump($model);




object(Comment)[39]

      private '_md' (CActiveRecord) => 

        object(CActiveRecordMetaData)[20]

          public 'tableSchema' => 

            object(CMysqlTableSchema)[24]

              ...

          public 'columns' => 

            array

              ...

          public 'relations' => 

            array

              ...

          public 'attributeDefaults' => 

            array

              ...

          private '_model' => 

            object(Comment)[19]

              ...

      private '_new' (CActiveRecord) => boolean false

      private '_attributes' (CActiveRecord) => 

        array

          'id' => string '10' (length=2)

          'author_id' => string '2' (length=1)

      private '_related' (CActiveRecord) => 

        array

          empty

      private '_c' (CActiveRecord) => null

      private '_pk' (CActiveRecord) => string '10' (length=2)

      private '_alias' (CActiveRecord) => string 't' (length=1)

      private '_errors' (CModel) => 

        array

          empty

      private '_validators' (CModel) => null

      private '_scenario' (CModel) => string 'update' (length=6)

      private '_e' (CComponent) => null

      private '_m' (CComponent) => null

Has class Comment a $title property?

No. But title is from another class. :unsure:

But it will be populated on this Comment class because it will be interpreted as some other table field. You have two options:

First and simpler, add a public $title property to Comment class.

Second and better, create a relation to the other table ActiveRecord class, and access to the title using $model->relation->title. See this guide to learn more about it.

The public $title its work, but the relation is not. :(

Can you show the code you’re using? (the relation’s declaration and its use)

As a general advice: An Active Record instance represents a row in a database table. Relational queries can be used to fetch a graph of related objects/rows and you can even do some aggregate stuff via STAT relations but Active Record is not primarily designed to perform arbitrary SQL queries with ad-hoc joins, groupings, etc. Plain SQL is far more flexible and easier to use for that.


'post' => array(self::BELONGS_TO, 'Blog', 'forum_id'),


$model->post->title

Thanks.