$models = Model::model->findAll( array( 'select'=>'t.*, rates.*', 'join'=>'( SELECT fid, SUM(pos) as pos, SUM(neg) as neg FROM rates GROUP by fid ) rates ON ( rates.fid = t.fid )') );
but you need to add attributes related to ‘rates’ columns in your model, so they will be filled with this query:
class Model extends CActiveRecord {
public $pos;
public $neg;
...
How about defining it in relations? Is this possible too? I see that I can do this using CDbCriteria too, but it would be really nice(and i think a little bit easier) defining it in relation rules
relation needs another ActiveRecord model on other side so it is necessary to create activerecord model for SQL query instead of table… quite interesting, but never tried such approach.
Well, in theory it is possible, but the point is that Active Record, while creating sql query, wraps table name into [html]table_name[/html]. If it would be possible to skip that graves, Active Record would possibly read SELECT statement as table.
I am not entirely sure if I understood you properly. Could you, please, describe it wider and little bit more clearly? Thank you in advance!
I’m also posting below a code, which returned me error “table couldn’t be found in database”.
class Rates extends CActiveRecord {
public static function model($className = __CLASS__) {
return parent::model($className);
}
public function tableName() {
return '( SELECT fid, SUM(pos) as pos, SUM(neg) as neg FROM rates GROUP by fid )';
}
}