Hi there,
please don’t mind if this sounds as a basic question, since I’m new here .
My problem:
For my model Concert and related bookings I want to get data about each category by this createCommand query:
$result = Yii::app()->db->createCommand()
->select('b.category, (b.nroftickets) AS count')
->from('tbl_booking b')
->where('b.concert_id=:concertId')
->group('b.category')
->order('b.category asc')
->bindParam('concertId', $concertId, PDO::PARAM_INT)
->queryAll();
I’ve defined the extra properties in my Concert model class (it is an AR model):
public $totalBookingsInA = 0;
public $totalBookingsInB = 0;
public $totalBookingsInC = 0;
When i view a concert (and it’s bookings) for the first time I get exact data, but in a mean time when I do another booking for that concert, I still do get the same data.
Here my ConcertController method:
public function loadModelWithBookingDetails($id){
$model= Concert::model()->with('bookings')->findByPk($id);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
//Yii::log(print_r($model, true), 'info');
//get nr of bookings
$raw = Booking::model()->getTotalbookingsPerCategory($id);
foreach ($raw as $actualrow)
{
if($actualrow['category'] =='A')
{
$model->totalBookingsInA=$actualrow['count'];
}else if($actualrow['category'] =='B')
{
$model->totalBookingsInB=$actualrow['count'];
}else if($actualrow['category'] =='C')
{
}
$model->totalBookingsInC=$actualrow['count'];
}
//Yii::log(print_r($model, true), 'info');
return $model;
}
When I execute the sql query directly into my MySql database I get the actual data:
SELECT b.category, sum(b.nrOfTickets) AS count
FROM tbl_booking b
where b.concert_id=_some_id_
group by b.category
order by b.category asc;
Could anyone help me here please? I’m lost :/.
Thanks in advance!
KR, AK