CakePHP tips

I’m using (& learning) CakePHP right now for my project. In the learning process, I’ve encountered plenty of issues, ‘wasting’ plenty of time to resolve them. You know, Google sometimes requires our time and patience to solve a ‘stupid’ problem. So for the sake of your convenience, I’ll update all the tips and tricks on troubles I’ve run into. Hope it helps.
(I only abridge the troubles and their solutions, yet include the links for your reference whenever necessary) 

  • passing data (from a controller) to elements: requestAction() + cache E.g. $comments = $this->requestAction(‘users/latest’); $this->element(‘comments’, $comment);
  • tricks: using compact() to pass data from a controller to a view more efficiently: $this->set(compact($var1, $var2, …));
  • Using MultivalidatableBehavior for multiple sets of validation rules for each model. It’s necessary when you have different forms or data that are all required to be validated (“differently”) in a model. E.g. Your User controller has login and register action, which should be validated by 2 (different) sets of validation rules. Recall that any fields of a particular validation rule in which ‘required’ is set to ‘true’ must be included in a form. Otherwise, this form won’t be validated using this set of rule.
  • Using PersistentValidation component component to keep your validation data after redirect, eg. you have multiple forms in a view, which relates to different actions (and even in different controllers). Now you want to display validation errors in this view instead of being redirected into the views of these actions.
Monday, April 4, 2011 around 2pm
Thursday, August 9, 2012 around 2pm
Posted in Programming. If you like it, show some