Snippets Snippets

Easy AJAX Pagination Using JQuery

By Casey Dreier
This simple little JQuery function allows you to drop paginated data into your views with ease.
[Read more]

Rebake all your admin views in one call

By Andy Dawson
A console script to recreate all admin views in a single call.
To install
ensure you are able to run the cake console commands
copy the below script into /app/vendors/shells/bake_admin.php

To run the script
at the command line:
cd /app/folder/is/here
cake bake_admin help
cake bake_admin all
[Read more]

Using your application svn revision number

By Andy Dawson
A little snippet to show how to find and use the svn revision number - to your advantage.
[Read more]

Visualize - Generate a graphic of your models/tables

By Andy Dawson
Based upon the script published by cakeexplorer (http://cakeexplorer.wordpress.com/2007/12/14/build-image-of-current-cake-schema), this script allows you to use Graphviz (http://www.graphviz.org) to generate a graphical representation of your models and/or tables

To install
ensure you are able to run the cake console commands
install graphviz
copy the below script into /app/vendors/shells/visualize.php

To run the script
at the command line:
cd /app/folder/is/here
cake visualize help
cake visualize

The output you'll find in the /app/config/sql folder

You can see a screencast if it in action here: http://www.ad7six.com/files/visualize.mpeg
[Read more]

Force login on Ajax requests

By Michal Szajbe
Users often encounter situations when they want to perform an action on a web site and get redirected to login page. Application’s task is to take care of performing the requested action right after user’s successful log in. It is easy job, unless the action is requested via ajax.
[Read more]

CakePHP & SMF

By Paul Kirkpatrick
A quick guide on getting SMF and CakePHP working together. Uses SMF for login, session handling, even layout.
[Read more]

Methods for turn on/ turn off speciffic behaviors.

By Yevgeny Tomenko
Sometimes there is appear situation when you need not some action which behavior do aftomatically using event handlers.
[Read more]

Haml: Markup Haiku

By Adeel Khan
Haml takes your gross, ugly templates and replaces them with veritable Haiku. Haml is based on one primary principal: markup should be beautiful. Check out http://haml.hamptoncatlin.com/.
[Read more]

Rewrite a plugin's paths to look nicer.

By Brandon Parise
Plugins are a fantastic and vastly underused tool in CakePHP! But their default paths can be strange sometimes. This article describes how to rewrite a plugin's paths using bootstrap.php.
[Read more]

Using CAKE_ADMIN for multiple user types

By Egbert Teeselink
A simple hack allowing to use the functionality of CAKE_ADMIN for more than one usertype.
[Read more]

Thumbnails generation with phpThumb

By Spout
phpThumb is a great thumbnail generator, it can generate thumbs with GD, GD2 or ImageMagick. There are many features like crop, rotate, watermark,... see all the features on the phpThumb homepage.
[Read more]

Taking Advantage of the Pages Controller

By Joel Stein
Looking for a few tips on how to make the Pages Controller your new friend? Out-of-the-box it automatically maps incoming requests to their views, sets the page title, and even handles sub-pages well. But wait, there's more!
[Read more]

Memcache wrapper class

By Jiri Kupiainen
A simple wrapper for PHP's Memcache functions
[Read more]

Extended beforeFilter() snippet

By Nate Constant
This is a simple addition to app_controller.php to allow a more customized beforeFilter() callback.
[Read more]

Confirm the admin route usage https

By Kyle Kai
While entering the admin route, needing to be confirmed must use https to carry on an on-line, in order not to the user neglect, and use the http on-line, so we have to make the check conversion automatically in the cakephp.
[Read more]

Using cakePHP data cache for caching enum values

By Mandy Singh
This example will show you how you can cache the enum values that you obtain from a database using cakePHP's data cache...
[Read more]

unbindAssociation

By Sean Callan
I needed a quick dynamic way to remove numerous associations I had in recent application. I decided the best to do that was through unbindAssociation(), so I made it.

This code was adapted from expects(), a function developed by Tom O'Reilly & expanded by Mariano Iglesias. As mentioned below I didn't want to maintain lists of models, so I adapted the code to remove associations instead.
[Read more]

Clean urls with isapi rewrite on iis

By Yuka Poppe
Forced to deal with a windows based server, without apache and effectively also mod_rewrite? But, after much complaining and begging your sysadmin has been kind enough to install isapi_rewrite on that (already bloated) iis box... then this is for you!
[Read more]

Redirects with Ajax

By sunertl
Redirects with Ajax are not really simple to do. When you redirect within the same controller, you can use setAction() instead but what if you want to redirect to another controller? I figured out a quite simple way using sessions, postet it - and got back the IMHO perfect solution by gwoo.
[Read more]

unbindAll

By Brett ODonnell
Here is a simple way to unbind all models. Just put this function into your app_model.php
[Read more]

Google Search using CakePhP

By Salim
There comes a time when everyone needs to search the Internet. This search can be done on Yahoo or Google or MSN or any other search engine.

This app utilizes Web Services to make a call against the Google Servers and returns the results to be displayed.

This is my first release of this app (based on an older cake ver.) and hopefully with the help of the community we can make changes and improvements to make this app a better and faster utility.

Thanks.
[Read more]

Easy peasy database config

By Joel Moss
Like a lot of developers out there, I use Subversion to keep control of my code and projects, and I also use a different database for development and production. But when using Cake this can be a problem when checking out my code from development to production. Unless I edit my database.php with my production config, the production code would have problems, as it would be trying to access data from the development database.

What I needed was an easy-peasy way of being able to check in my code to production without having to edit the database.php config file. So what I did was very simple and can be found below.

For a full write up of this trick and other Cake stuff, please see my Blog at http://joelmoss.info
[Read more]

dAuth v0.3 views controller and javascript

By Dieter Plaetinck
the login, register, userinfo and change_password views; the usersController, and d_auth, sha1 javascripts for dAuth v0.3
[Read more]

Using NeatString To Make Random Passwords

By Matt Stith
In my hunt for a simpler way to generate a random password, I ran across a little class that CakePHP already has.
[Read more]

Get Next Auto Increment

By James Connell
A small hack that returns the auto increment id for the current model.
[Read more]

Pagination Element

By Andy Dawson
The simple pagination element. For instruction on use see: http://bakery.cakephp.org/articles/view/65
[Read more]

Limit the models used in find operations

By Matt Keller
Often when issuing a find, findAll or other find*-variety there's way too much data returned which puts unneccessary overhead to your database server or it might pose a security risk when some crucial fields like passwords are available to the view designers just because the users-table was included in your findall...
[Read more]

Show and Hide Debug SQL

By Chris Martin
Place an Element on your page or layout to toggle view of the SQL messages (DEBUG >= 2).
[Read more]

Baked Enums

By John Zimmerman
Moving this snippet over from CakeForge.

This is a preliminary solution to solve the problem of baking your Models's, View's and Controller's with enum keys in the database.

It is not completely automatic and I can only tell you that it works with PHP5 and MySQL 4/5.

As PHPNut stated in trac, the enum columns are too different across databases to make this standard (at least for now).

So if you are using something other than MySQL you will have to change the code in app_model.php accordingly.
[Read more]

getDSN

By John Zimmerman
This returns the database connection details in the form of a DSN.

driver://username:password@hostname/database

This is useful if you have a 3rd party class that goes in /vendors that requires its own DSN information to work. (i.e. GoogleMapAPI class).

I put this function in /app/app_controller.php so that it is available to all other controllers if necessary.

Originally based on a post in the GoogleGroup .
[Read more]