As a developer, I am always cautious about allowing a framework to automagically do things for me. When I was a web developer at
Sporting News, our development staff spent a lot of time analysing different PHP frameworks and open source CMS packages. We were searching for a starting point, whether it was a framework or a CMS, upon which we planned on building the latest version of www.sportingnews.com.
After we settled on CakePHP, we knew out of the gate that it came with some things that we wouldn't be able to tolerate. The initial issue was the lack of bind variables in the Oracle connection wrapper class. I've addressed this issue numerous times and have posted my solution
here.
The second issue were the sheer amount of queries that CakePHP generated for selecting data from related tables. Initially, we were only internet experts(someone that becomes and expert on a subject matter from reading things on the internet) and basically self-taught ourselves to use CakePHP. This has it's pros and cons, but I look back now and wish the cons weren't such a large list. Our self taught solution to the massive amount of queries that were being generated was to unbind everything that wasn't being used. However, if a new model is related to the model doing the "find", then every place the model is used, will require more unbind code to be added. This quickly becomes extremely unmanageable.
Recently, I found a tutorial on
CakePHP's Containable Behavior. This has to be one of the most powerful pieces of CakePHP that we initially missed. The Containable Behavior allows a developer to target the exact fields from only the specified tables and CakePHP generates one query to gather the data.
In my Prescriptions controller, I needed to generate a data set from different models that I could then run through CakePHP's pagination system.
$this->Prescription->Behaviors->attach('Containable');
$this->paginate = array(
'conditions'=>array(
'Prescription.file_id'=>$id,
'Prescription.status' => 1
),
'limit'=>20,
'order'=>'MyFile.created DESC',
'fields'=>array(''),
'contain'=>array(
'MyFile.id',
'MyFile.name',
'MyFile.created',
'Program.name',
'Program.program_date',
'Patient.first_name',
'Patient.last_name',
'Patient.patient_id'
),
);