zen of coding

Incrementing a field in CakePHP

Let’s say you have an application, where users can place votes for their favorite products and you’d like to increment the current number of votes by one. It’s very easy by using updateAll: $this->Product->updateAll( array(   ‘Product.vote’ => ‘Product.vote+1′ ), array(’Product.id’ => 40)); You may want to restrict Product model[…]

Read More »

CakePHP cookbook 1.2 (as of 05/21/2008) in PDF format

Update: 6/09/2008 Unforunatelly I cannot have this PDF file here anymore, since it is a violation of the license. The good news is that PDF export will soon become a part of the official cookbook site, so stay tuned…

Read More »

Remember $this!

Do you need to get some information about your current state of the cake app?… Maybe find out the name of the current layout? Or see what helpers are currently loaded? Need session time?… etc., etc. $this contains a lot of usefull information. Just try it in your beforeRender() in[…]

Read More »

Build a string from a resultset array

Sometimes it’s very handy to be able to quickly build a string from CakePHP’s resultset array. For example you want to build a string of ID’s to be used with the “IN” condition in your DB (such as Profile.id IN(1,2,3,4)… ). Let’s say we did a find(‘all’… and got an array[…]

Read More »

I need the first record from my table…

Let’s say you need to know when was the very first user record created. Add a method like this to your User model (assuming you have a ‘created’ field): [sourcecode language=’php’] function getFirstDay() { return $this->find(‘first’, array( ‘fields’ => array(‘created’), ‘order’ => ‘User.created ASC’)); } [/cc] Try in the controller:[…]

Read More »

Array from XML in CakePHP

Update (9/9/2008): Since the writing of this post, there is now a nicer way to handle this, which David Persson kindly pointed out: $Xml = new Xml($rawXML); $Xml->toArray(); For historical reference and if you don’t have the latest and greatest CakePHP core, here’s the “old method”… On IRC gwoo posted[…]

Read More »

amCharts with CakePHP

There is a very slick flash charting tool out there called amCharts (www.amcharts.com). If you are in need of some reporting or charting UI for your application, I strongly recommend it. This is a quick guide on how to get it working with CakePHP. For this example, I’ll show you how[…]

Read More »

CakePHP and custom SQL

If you’ve been around CakePHP for some time, you’ve probably heard that writing custom SQL is pretty much frowned upon. At the core, CakePHP provides some clever ways to write queries by employing the find() and save() methods, yet in some cases it is just impossible to use find() or save()[…]

Read More »

Redirecting to the home page

Sometimes you’d like to create a link that will redirect your traffic to the homepage. For example, you have some partner web site that will drive traffic to a URL like: www.yoursite.com/partnerName. This way you’ll be able to track this link in the web site logs. Just add the following[…]

Read More »

Don't forget your primary key

If are you are using a non-standard column name for your primary key (something other than ‘id’), maybe from a legacy database, it is very important not to forget to set the var $primaryKey in your model. This little oversight can break a lot of things without giving any clue[…]

Read More »
%d bloggers like this: