Speeedfire
(Speeedfire)
September 18, 2012, 3:19pm
1
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
gbasto
(Goncalo Basto)
September 18, 2012, 3:41pm
2
Has class Comment a $title property?
Speeedfire
(Speeedfire)
September 18, 2012, 4:02pm
3
No. But title is from another class.
gbasto
(Goncalo Basto)
September 18, 2012, 4:20pm
4
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.
Speeedfire
(Speeedfire)
September 18, 2012, 4:47pm
5
gbasto:
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.
gbasto
(Goncalo Basto)
September 18, 2012, 4:54pm
6
Can you show the code you’re using? (the relation’s declaration and its use)
phtamas
(Phtamas)
September 18, 2012, 6:18pm
7
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.
Speeedfire
(Speeedfire)
September 19, 2012, 6:23pm
8
'post' => array(self::BELONGS_TO, 'Blog', 'forum_id'),
$model->post->title
gbasto:
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.
Thanks.