+91 9891501300

Latest Post


Please enter your details below and we'll call you back shortly!
CakePHP Useful TIPS and Tricks
CakePHP Useful TIPS and Tricks
CakePHP Useful TIPS and Tricks

CakePHP Useful TIPS and Tricks

CakePHP Useful TIPS and Tricks


  1. Save() does not work!

Sometimes it takes place that save() falls short with no apparent reason. Your data variety looks great and also you have actually build the form appropriately, etc., and so on, however no question was carried out. It is really feasible that Save had actually failed as a result of validation mistakes. Maybe you are updating some version and while the existing fields in the kind pass the validation, there is a possibility that some “other ones” are triggering the validation regulations to stop working. A very easy (and valuable) means to see exactly what’s going on with validation is to do pr($ this- > validationErrors); in your sight. By using this approach you’ll see exactly just what’s occurring with your model’s recognition. The various other option is to pass false as a 2nd parameter to Save(); in order to disable the recognition. However, the latter technique does not give much of tip and also should not be utilized to take care of a failing trouble, but instead to deliberately prevent recognition.


  1. Save() still does not work!

Do you have beforeSave(); in your version or application model? Constantly ascertain for this method, and even more importantly, make sure that it returns true.


  1. Validating on create or update

CakePHP has an ‘on’ key to be made use of in your $validate range. It allows you to define whether the guideline ought to be enforced throughout a new document development or during an update of an existing record. As an example, if you only want to look for a special e-mail address when you are producing a brand-new Customer account, you’d include ‘on’ => ‘produce’ to your $validate selection. As a result, this rule will be overlooked when you are updating/editing some customer.


  1. Mind your cache

You’ve made some adjustments to your tables, however your application “disregards” them … You have actually moved  some points around, bur your application does not seem to “discover” that …

Clear the cache. Remove the data from your app/tmp/cache. Make sure to only delete the files and also not the directory site framework.

Basically if you discover some unusual behavior in your application, just keep in mind that maybe triggered by the cache.


  1. I’m shedding the extra URL specifications when paginating

You need to keep some extra LINK criteria throughout pagination. For instance the LINK is something like/ products/view/45. Yet, when you build the pagination the ID (45) is lost … Well, all you have to do is include this line of code to your view: $paginator->options(array(‘url’ => $this->passedArgs));

Update: this is likely repaired in the current builds of CakePHP, yet still excellent to be familiar with.


  1. Making use of afterFind().

Model:: afterFind() permits you to do some information manipulation after the Design’s discover approach. Right here’s an example use in the model:.

function afterFind($results, $primary=false) {
if($primary == true) {
// do stuff to the $results

return $results;


$ primary will be readied to real after your find() technique is executed and some outcomes are returned. After you change the $results you will certainly return them back to your application (controller).


  1. I need to know the standard details regarding my table.

Try this: pr($this->ModelName->schema())


  1. Exactly how do I look for a non-empty field in CakePHP 1.2?

Update: cake core currently has a ‘notEmpty’ policy built-in, so definitely use it instead.

For historic functions only:.

The old VALID_NOT_EMPTY constant is now deprecated and there does not appear to be a policy to change it … Well, it’s easy sufficient using: ‘rule’ => array(‘minLength’, ‘1’)

Martin Bavio pointed out that having space personalities (only) as your field information will make the recognition guideline pass as well as this is most likely not a preferable impact. Utilizing this basic regex, rather, will capture a vacant string with area characters: ‘rule’ => array(‘custom’, ‘/\S+/’)


  1. Stay clear of utilizing the $uses array.

You’ve got two totally unrelated models, yet you need details from one in the controller of an additional. The initial idea is to add them to the $uses selection. Hey, it’s very easy and finishes the job. Well, to earn the long story short, it’s bad technique. Think of your model bindings as well as make certain that models are really unrelated to each other. In some cases your Customer version is definitely not related to CommentRating, yet you definitely need it in your users controller. Well, simply by chance it shows up that User- > Message- > Remark- > CommentRating. It’s a deep binding that may not be apparent at first, yet by utilizing such a chain of designs you can easily avoid utilizing the $makes use of array, when it’s truly not essential.

Update: if you truly should load some arbitrary (and also certainly unconnected) design in your controller, do it like so in the given action:.

As soon as this line executed you’ll have an instance of MyModel available in $this->MyModel (pr($this->MyModel); to see your freshly loaded object.

Another option is to use this approach:
$MyModel = ClassRegistry::init(‘MyModel’);

One more choice is to use this technique:.

$ MyModel = ClassRegistry:: init(‘ MyModel’);.

Which behaves, since it permits you to do points similar to this:.

$someData = ClassRegistry::init(‘MyModel’)->find(‘all’, array(‘conditions’ => array(‘ LIKE’ => ‘% ‘. $name . ‘%’ ))); (the latter, chaining, example is PHP5 only)


  1. Clean-up ugly HTML.

It’s no secret that CakePHP commonly outputs some extremely awful and also difficult to read HTML. This little method will make it a whole lot cleaner.

Produce a documents called app_helper. php in your app’s root directory.

Following include this feature to it:.


function output($string) {
return parent::output($string . “\n”);

Take a look at your HTML currently … far better.


  1. How can I access session data in the Data View?

Very easy. You have the $session helper available.
Just try it: pr($session)
Update: if you have already written some variables to the session, then try pr($session->read());


  1. I need to save data from numerous Models.

Do not also consider utilizing loopholes and also other trickery. There is a very wonderful (however not well recorded) approach saveAll() offered. It will let you save information from multiple versions simultaneously. Approved, it takes a little time to figure it out, once you get it working it is actually a time saver. Try using it with some dummy data, and make sure that your data selection is correctly formatted and also organizations are configuration correctly.


  1. Static pages and also Routes.

I truly don’t such as having/ web pages/ as component of the LINK for my static pages. Well, they are static … so allow’s make them.html instead (at the very same time we sprinkle just a little of security by obscurity rule).

If you had links pointing to they need to now point to and also include this to your routes:.

Router::connect(‘/(.*).html’, array(‘controller’ => ‘pages’, ‘action’ => ‘display’));



  1. The fastest way to build a form in CakePHP.


echo $form->create();
echo $form->inputs();
echo $form->end();

It is simply a step beyond scaffolding, however really this is just how easy it is to build a kind in CakePHP. Just give it a go.


  1. Get to know CakePHP using Github.

CakePHP is frequently progressing as well as if you are serious about creating in addition to this framework it is essential to stay on par with the most recent as well as best and also to obtain accustomed to several of the new as well as forthcoming attributes. is a great area for this.


Top Courses

Learn More About Multimedia & Increase You Knowledge .



Most In Demand Multimedia Softwares To Learn From Bapu Graphics .



Most Popular Languages to Learn Development Skills From Bapu Graphics .

Published: June 23, 2018
Writen by
bapu graphics logo


Please enter your details below and we’ll call you back shortly!

bapu graphics logo

Book Your Free Counselling Session

Please enter your details below and we’ll call you back shortly!