There are many things that come to you as surprise when you develop something. Of course, we are talking about the not-so-good surprises that baffle you at times with the kind of response you get while you are not at all ready for it. Based on my experiences I have a few tips to stem them up and get on with it easily. Here are the most common of them:
1. Not able to validate on create or update
Many a times we face a lot of difficulties with validation when we are just about to complete. This is really irritating and gets a hell out of you. Better if avoid it right away in the beginning when you start creating or updating a record file. For this, just put the $validate array on, when you plan to work on the records. This would save a lot of your efforts later on if any entry goes wrong.
2. Not able to get basic information about the table
There are lousy instances like this where you just go sick tumbling on little issues with nothing working for you. Hold on! Just try putting this: pr($this->ModelName->schema()) and you will get the information about the table. It is this easy!
3. Problem with Table Entry
Changes done on table can get a bit hard to manage for the program if they don’t go as per the predefined format. You face situations when you go on doing changes on table entries and your app doesn’t take them. All you need to do here is clear the cache. Delete the file from app/tmp/cache and you will get rid of all the strange things happening out there.
4. Accessing data session in the view
With the $session helper available, all you need to is just put in pr($session). If there are already some variables placed in the session, try pr($session->read()) and you will be through with it.
5. Losing the extra URL parameters when paginating
This is normal when you are doing some random pagination task. For a URL, say, /products/view/45, if you lose the ID (45) add this line of code to your view: $paginator->options(array(‘url’ => $this->passedArgs)). This will fetch you all the required details without a moment of strain.
6. Save() does not work!
It get annoying when you pass a command in CakePHP and it does not work as per what your learning guide says. It happens with Save() a lot. If you ever face this error, go back and check the validations you have put. It is possible that some of the models your entered are failing to satisfy the validation rules and causing this error. An easy and fast way to do this is placing this command across pr($this->validationErrors); in view. This will instantly show what error it is and you will find an easy way out immediately.
7. Save() still does not work!
If you still see it not working, check if you have beforeSave() in your model or app model. Double-check for the ‘true’ value if you still can’t see it. You will get it this time for sure!
8. Checking for a non-empty field in CakePHP 1.2
Well, now you have the ‘notEmpty’ rule built-in with the new CakePHP release. So, the old VALID_NOT_EMPTY constant is not favored anymore by the users. But, as you have no option to replace it with anything, it’s better to use ‘rule’ => array (‘minLength’, ‘1’) to get rid of it.
9. Using afterFind()
If in case you need to do some data manipulation and you are finding it difficult to do it using afterFind() do it after Model’s find method, applyingModel::afterFind().
These few lines of code would help you get it done:
Once you pass this code and the find() method is executed, the $primary function will be set to trueand the results will be returned back to your app.
10. You find it tough to build a form in CakePHP
Just enter this code and you have your CakePHP form in front of you. Just a step beyond scaffolding. Try it and you will have no complaints with form building any more.
Note: The tips described in this article is only applicable for CakePHP version 1.x and latest 2.x versions of CakePHP.