Richard's blog

Richard's picture

MySQL: Disable foreign keys while updating database

Originally found here: http://www.stetsenko.net/2008/10/mysql-how-to-ignore-checking-of-foreign-key-constraints-for-innodb-tables/

Making a note of it here because it always take me a while to find the above article.

You can switch off checking of foreign keys while you truncate/populate the tables in a MySQL database with:

SET FOREIGN_KEY_CHECKS = 0;

and re-enable it with:

Richard's picture

CakePHP Quick tip: Adding CSS links to $scripts_for_layout in 1.3

Got caught with this one upgrading from an older version of CakePHP to 1.3

In your view, to add a css file to the $script_for_layout variable, you used to do this:

<?php $html->css("css_file", null, array(), false); ?>

In 1.3, you have to change this to:

Richard's picture

Quick CakePHP Tip: Prefix/Admin Routing and form->create()

A quick tip to get your forms to play nice with admin routing:

/views/users/edit.ctp

<?php echo $form->create("User", array("url" => $html->url(array("admin" => true), true))) ?>

Why? For some reason, $form->create() ignores the usual routing prefix attribute ("admin"=>true) so you have to pass the url through $html->url() which does take prefix routing into account.

Richard's picture

einstein_www


einstein_www
Originally uploaded by richardathome

 

Richard's picture

CakePHP: Extend PaginatorHelper to indicate sort field and direction

Here's a quick piece of code that extends the default CakePHP PaginatorHelper
to add css classes to the sort column links to indicate which field is being sorted by,
and which direction:

/app/views/helpers/ex_paginator.php:

Richard's picture

CakePHP: Pagination with Containable

Took me a little while to work this out so I'm blogging it here in the hopes it will help someone in the future:

Need to Contain your Paginated data?

in your controller:

$this->paginate = array(
	"contain"=>array(
		// usual contain array
	)
);
 
$this->set("data", $this->paginate("Model"));

For example, to bring back and paginate a User (id, first_name, last_name) and their Title (Mr, Mrs, etc.):

/app/users_controller.php

Richard's picture

Must Have Software

I'm about to put together a new PC at home. Here's a mental reminder to myself about what software I can't live/work without:

Richard's picture

CakePHP: Smarter links

The Problem

There's a couple of things I wish were present in CakePHP's excellent HtmlHelper.

  1. Links should automatically add a class to themselves when they are a) pointing to a resource in the same controller and b) pointing to themselves.
  2. Able to show/hide themselves.

Why is #1 important? Imagine a primary navigation that runs across the top of a page:

Richard's picture

CakePHP: Auto populating foreign key dropdown fields

One of the neet features of CakePHP's scaffolding is that it automatically populates your foreign key (belongsTo, hasAndBelongsToMany) fields in your forms.

Lets take a look at a simple example to illustrate this:

app/models/article.php:

class Article extends AppModel {
	var $hasAndBelongsToMany = array(
		"Tag"
	);
}

app/models/tag.php:

Richard's picture

GeekUp Sheffield: The Aftermath

Last nights GeekUp was a lot of fun. Finbarr's presentation on making Agile pay was very informative and my talk on CakePHP went off without a hitch (apart from a minor technical hiccup with the hardware at the start).

As my first presentation, I was looking to learn as much as I could from the experiance. Here's what I came away with:

Syndicate content