Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unknown column - multiple joins in CDbCriteria

I'm trying to get data from multiple tables and I've ended with this error: SQL: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'p.firstname' in 'field list'

    $criteria = new CDbCriteria;
    $criteria->select = 'ohu_id, hash, p.firstname, p.surname, p.city, u.email AS Email';
    $criteria->join = 'LEFT JOIN `profiles` p ON  p.user_id = user_id';
    $criteria->join = 'LEFT JOIN users u ON user_id = u.id';
    $criteria->condition = 'offer_id = :oID';
    $criteria->params = array(':oID' => $_GET['id']);

    $model = MyModel::model()->findAll($criteria);

Anyone know what I'm doing wrong? Or is there better way to get related data?

like image 741
PsychoX Avatar asked Dec 22 '11 13:12

PsychoX


1 Answers

You are making the same mistake I made hehe.

You are overwriting the first join with the second one, instead of appending the second join.

$criteria->join  = "join ...."; //first join
$criteria->join .= "join ...."; //second join

cheers

like image 82
Felipe Avatar answered Oct 24 '22 21:10

Felipe