user3434212
user3434212

Reputation: 87

Cannot do Inner join in CDbCriteria

This is mysqli query

SELECT DISTINCT t.company_id,t.image,t.text,t.date, t.title AS c_title
FROM news t
INNER JOIN companies c ON c.company_id=t.company_id ORDER BY t.date DESC
LIMIT 20" or die ("ERROR ". mysqli_error($link));

I want to write in CDbCriteria

$Criteria = new CDbCriteria();
$Criteria->join = 'INNER JOIN companies c ON t.company_id=c.company_id';
if ($place>0){
    $Criteria->condition = "t.company_id = :place";
    $Criteria->params = array(':place'=>$place);
}
$Criteria->order = "t.date DESC";
$Criteria->limit = 20;
$Criteria->select='t.company_id,t.image,t.text,t.date,c.title AS c_title';
$dataProvider = new CActiveDataProvider('News',
    array(
        'criteria'=>$Criteria,
        'pagination'=>false
    )
);

Error Property "News.c_title" is not defined

Upvotes: 0

Views: 297

Answers (1)

davey
davey

Reputation: 1791

The property c_title is not defined in your News model (fields from your news table will be available as properties automatically, but c_title is an alias of title and not a field of of your table).

Put this in your News model:

public $c_title;

Upvotes: 1

Related Questions