AR relations randomly failing

Hello everybody

I am in the latter stages of building quite a fancy application, and I have run into an issue I cannot wrap my head around.

In a very oversimplified summary, the application does the following:

  1. Entities providing specific training courses adds their courses on offer

  2. Organizations add their staff members as trainees, and chooses courses for their staff from a pool of approved courses

  3. These trainee-course combinations, once approved by administrators of a governing body, generates a voucher

  4. The voucher is then redeemed by the trainee at the training provider, at the expense of this governing body

One integral feature of the app is that it generates these vouchers in PDF (using dompdf) format.

  1. An action called <controller>/topdf/id is called, where id identifies a voucher record in the (mySQL) database

  2. The AR model is loaded, still using the generic loadModel($id) method so courteously generated by Gii

  3. The model is fed to a view which generates a simple HTML table, which is later then processed by dompdf

  4. In my Voucher model, I have declared relations like follows in its relations() method:

     	...
    
    
     	'sme'=&gt;array(self::HAS_ONE,'Sme','id'),
    
    
     	'provider'=&gt;array(self::HAS_ONE,'Provider','id'),
    
    
     	'trainee'=&gt;array(self::HAS_ONE,'Trainee','id'),
    
    
     	'course'=&gt;array(self::HAS_ONE,'course','id'),
    
    
     	'coursedate'=&gt;array(self::HAS_ONE,'CourseDate','id'),
    
    
     	...
    
  5. In my view that builds the voucher html markup, the voucher object is passed to the view simply as ‘$v’ in the controller, using

$this->render(‘template_default’,array(‘v’=>$voucher),true);

It returns the view markup to a domPDF function, thus the ‘true’ switch in the render method.

Theoretically, I should be able to access my related models of a voucher like $v->sme->… , $v->trainee->… and so forth in the view, right? Wrong.

For some reason, when generating the view, Yii would fail to load the related models when I access them in this fashion. Strangely, it fails on different related models at different times - one page load to the next. Sometimes it fails on the ‘sme’ relation, sometimes on the ‘trainee’ relation, and so forth - seemingly randomly.

Failing to load the model of course results in a ‘trying to get a property of a non-object’ error when I access the related model’s attributes. Checking the Yii application log (CWebLogRoute), I can see Yii executing the query that the retrieves the model that causes the error, but it fails nonetheless right after the query is executed.

What boggles me, is that sometimes it works for a given model/relation, and sometimes it doesn’t.

I have found a suitable work around to this issue for the time being, using custom methods that retrieves the models ($v->getTrainee()->…), but I would appreciate any thoughts on why this might occur.

My development environment is a Windows 7 (I know) box running Wamp with

  • Apache 2.2.11

  • PHP 5.3.0

  • MySQL 5.1.36

and using Yii 1.1.6.

I have been enjoying Yii in building apps for the last 4 months, without ever running into any troubles I could not resolve from the forums or a bit of actual brain power, but like I said, this boggles me.

Your thoughts is much appreciated.

Correction:

‘course’=>array(self::HAS_ONE,‘course’,‘id’), should be ‘course’=>array(self::HAS_ONE,‘Course’,‘id’),, following my model naming convention.