Iterating Through Related Model.

I have a ‘reservation’ that has ‘reservationDetail’ records (each reservation has many reservationDetails). For some reason I can’t iterate through the list of detail records. Obviously I’m doing something wrong…

Reservation relations




public function relations()

	{

		

		return array(

			'eventRecord' => array(self::BELONGS_TO, 'EventRecord', 'event_record_id'),

			'reservationStatus' => array(self::BELONGS_TO, 'ReservationStatus', 'reservation_status_id'),

			'reservationDetails' => array(self::HAS_MANY, 'ReservationDetail', 'reservation_id'),

		);

	}




ReservationDetail relations





public function relations()

	{

		return array(

			'rate' => array(self::BELONGS_TO, 'Rate', 'rate_id'),

			'reservation' => array(self::BELONGS_TO, 'Reservation', 'reservation_id'),

                         'rateClass'=>array(

                               self::HAS_ONE, 'RateClass', array('rate_class_id'=>'rate_class_id'), 'through'=>'rate')

                        

		);

	}







I’m trying to do something like this:




                   $cleanUpMinutes = Config::getValue('reservation_timeout_minutes');

                    $criteria = new CDbCriteria();

                    $criteria->addCondition('reservation_status_id=3'); //3 = pending payment

                    $criteria->addCondition("NOW() > mod_date + INTERVAL $cleanUpMinutes MINUTE");

   

		   $staleReservations = Reservation::model()->with('reservationDetails')->findAll($criteria);


          foreach($staleReservations as $reservation) {

               //want to delete the detail records (reservationDetails) and the primary model here as well.

         }






Trying to loop through the details keeps giving me “trying to get property of non-object” error. I think I have something wrong in my relations based on output of CVarDumper::dump of $staleReservations. I’m just a beginner, but it doesn’t look right…

Right now i have one record in reservation and 3 associated records in reservation detail.

please, try to add to your criteria


$criteria->together = true;

Tried that. Does not seem to help.

Strange thing is… I can look up the detail records separately and access $reservationDetail->reservation with no problems using the same criteria, but it makes more sense to do it as $reservation-> then iterate through detail records.

So, this works:





//$reservations = Reservation::model()->with('reservationDetails')->together()->findAll($criteria);  //does not work.

                   

                   

                   $details = ReservationDetail::model()->with('reservation')->findAll($criteria);  //works fine

             

                   foreach($details as $detail) {

                       

                       echo $detail->reservation->reservation_id;

                   }

I can access it by looking at the details first then getting ->reservation, but not the other way around…

Please try:


   foreach($staleReservations as $reservation) {

                if($reservation->reservationDetails){

                     echo $reservation->reservationDetails->someValue;

                }

         }



Does not return any value. In this case, it looks like $reservation->reservationDetails is not populating…

But this works… Very bad form but I was just trying to figure out why $reservation->reservationDetails is not working.




 foreach($staleReservations as $reservation) {

                        if($reservation->reservationDetails){

                              echo $reservation->reservationDetails->quantity;

                     } else { echo 'not valid';   }  //returns 'not valid' in testing.

                   }

                   

                     foreach($staleReservations as $reservation)

                         { 

                         $details = $reservation->getRelated('reservationDetails');

                                foreach($details as $detail)

                                {

                                    echo $detail->quantity;  //returns the correct quantity for the two values...

                                }

                            

                           }  




I just do not understand why


$reservation->reservationDetails

does not work, but iterating through $staleReservations then calling


$reservation->getRelated('reservationDetails')

works…