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:
Entities providing specific training courses adds their courses on offer
Organizations add their staff members as trainees, and chooses courses for their staff from a pool of approved courses
These trainee-course combinations, once approved by administrators of a governing body, generates a voucher
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.
An action called <controller>/topdf/id is called, where id identifies a voucher record in the (mySQL) database
The AR model is loaded, still using the generic loadModel($id) method so courteously generated by Gii
The model is fed to a view which generates a simple HTML table, which is later then processed by dompdf
In my Voucher model, I have declared relations like follows in its relations() method:
... 'sme'=>array(self::HAS_ONE,'Sme','id'), 'provider'=>array(self::HAS_ONE,'Provider','id'), 'trainee'=>array(self::HAS_ONE,'Trainee','id'), 'course'=>array(self::HAS_ONE,'course','id'), 'coursedate'=>array(self::HAS_ONE,'CourseDate','id'), ...
In my view that builds the voucher html markup, the voucher object is passed to the view simply as ‘$v’ in the controller, using
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
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.