Reputation: 5094
When I am trying to fetch the results using CDbCriteria the It does not give me the expected results where as when I run the same query that is generated by CDbCriteria in the phpmyadmin then I get the results. I do not understand whats happening.
Criteria is as follows :-
$criteria = new CDbCriteria();
$criteria->select = 't.*c.*' ;
$criteria->join = 'LEFT OUTER JOIN `flight_cancel` as `c` on t.ticket_id = c.ticket_id';
$criteria->compare('t.booking_id', $bookingId);
$data = FlightTicket::model()->findAll($criteria);
Whereas the query is
SELECT t.*c.* FROM `flight_ticket` `t` LEFT OUTER JOIN `flight_cancel` as `c` on
t.ticket_id = c.ticket_id WHERE t.booking_id= 'something'
When Using CDbCriteria It only returns me the flight_ticket data and not the flight_cancel data.
What I am doing wrong ??
Upvotes: 0
Views: 97
Reputation: 14860
$data = FlightTicket::model()->findAll($criteria);
will return only FlightTicket
records. Since flight_cancel
has an ActiveRecord class say FlightCancel
and a relation flightCancel
in FlightTicket
you can alter your criteria to include this:
$criteria = new CDbCriteria();
$criteria->together = true; // perform eager loading
$criteria->with = array('flightCancel' => array('joinType'=>'left outer join'));
$criteria->compare('t.booking_id', $bookingId);
$data = FlightTicket::model()->findAll($criteria);
You can then access your FlightCancel
data through $model->flightCancel
(or $model->flightCancel[]
for a ONE-TO-MANY relation) where $model
is a FlightTicket
record.
EDIT
From the yii database guide
AR relies on well defined primary keys of tables. If a table does not have a primary key, it is required that the corresponding AR class specify which column(s) should be the primary key by overriding the primaryKey() method as follows,
public function primaryKey() { return 'id'; // For composite primary key, return an array like the following // return array('pk1', 'pk2'); }
Therefore without a primary key, you are better off using CDbCommand::queryAll()
to return the result of the query as an array of rows of the result.
Upvotes: 2