search
top
Currently Browsing: (Cake)PHP

Regex to match specific HTML-tags

<tag\b[^>]*>(.*?)</tag>

Add a leading zero to an integer (useful for month numbers) in PHP

This loop prints every month number with leading zero’s:

for($i=1; $i <= 12; $i++):
     echo sprintf('%02s', $i);
endfor;

CakePHP e-mail displays weird characters (=0D=0A=0D=0A)

Cause: you’re sending your e-mail as HTML only.
Solution: send the e-mail as “both”, like this:

$this->Email->sendAs = 'both';

Prevent data from being overwritten in your controller save action

Before you perform your save, execute a create first. This prevents data from being overwritten due to an ID being set that you didn’t know about.

$this->Model->create();
$this->Model->save($aYourData);

How to use a different (second) database in CakePHP

/app/config/database.php code:

var $connectionName = array(
           'driver' => 'mysql'
     ,     'persistent' => false
     ,     'host' => 'localhost'
     ,     'login' => 'your-username'
     ,     'password' => 'your-password'
     ,     'database' => 'your-database'
     ,     'prefix' => ''
);

In a random model:

$this->useDbConfig = 'connectionName';

top