Forcing cake to use “dashed” HTML DOM ID’s

Why bother?

I guess just to keep CSS purists happy, we should have our DOM id’s appear as “some-field-id” rather than CakePHP’s default “SomeFieldId”.
(Also, it’s just a matter of consistency and most certainly personal preference).
There is no defined rule for this, but in overwhelming CSS examples and tutorials it is quite common to see “dashed” names rather than camel cased.

Thanks to a tip from Mark Story, we can override the default behavior by creating an app_helper.php with the following method:

public function domId($options = null, $id = 'id') {
  $view =& ClassRegistry::getObject('view');
  if (is_array($options) && array_key_exists($id, $options) && $options[$id] === null) {
    unset($options[$id]);
    return $options;
  } elseif (!is_array($options) && $options !== null) {
    $this->setEntity($options);
    return $this->domId();
  }

  $entity = $view->entity();
  $model = array_shift($entity);
  $replacement = array($model .' '. implode('-', $entity));
  $dom = $model . join('', array_map(array('Inflector', 'camelize'), $entity));	
  $dom =  preg_replace('/(?<=[^A-Z])([A-Z])/', ' $1', $replacement);
  $dom = strtolower(implode('-', Inflector::slug($dom, '-')));
  if (is_array($options) && !array_key_exists($id, $options)) {
    $options[$id] = $dom;
  } elseif ($options === null) {
    return $dom;
  }

  return $options;	
}

This overrides Helper::domId() and gives us nice “model-field-name” ID’s with dashes rather than “CamelCase”.
The actual change happens in lines 13 – 16 (if you care), the rest of the method remains the same as the core.

Important database session limitation

If you are using a database to keep your sessions, there is one rather significant limitation which you should be aware of.

It does depend on the specifics of your DB, but I am going to guess that in most cases it is something that could happen to any database…

For example, we were using MySQL to keep the session data.
Looking at the table you’ll see that session data is serialized and stored in the “data” field…

By default that field is set to MySQL’s “TEXT” type, which has a size limitation of ~ 65K.

I know, I know… why would you want sessions larger than 65K?
Well…

“640k ought to be enough for anybody”

So, just in case, you do need a larger session the remedy is pretty simple, change the field type to “MEDIUMTEXT”, which effectively gives you ~ 16MB.

p.s. If you need even a larger size you can go with “LONGTEXT”.

For your reference, here’s the semi-official breakdown of different text-type fields in MySQL and their limitations:

TEXT
A BLOB or TEXT column with a maximum length of 65535 (2^16 – 1) characters

MEDIUMTEXT
A BLOB or TEXT column with a maximum length of 16777215 (2^24 – 1) characters

LONGTEXT
A BLOB or TEXT column with a maximum length of 4294967295 (2^32 – 1) characters

Couple of convenience methods to add to your bootstrap.php

Although most of the convenience methods will be removed from CakePHP going forward (see the note about basics.php), and I say good riddance, you still have the opportunity to add your own to bootstrap.php.

For example a couple that I like are wrappers for var_dump(). The main reason being is that var_dump() will properly display NULL and FALSE. Unlike debug() and pr() (alias to) print_r(), which will show… well… nothing.

So here you go:

function vd($var) {
  if(Configure::read() > 0) {
    echo '<pre>';
      echo var_dump($var);
    echo '</pre>';
  }
}

Or, if you’d like to die() out:

function vdd($var) {
  if(Configure::read() > 0) {    
      die(var_dump($var));
  }
}