Aug 28: Alternative unbindModelAll
class AppModel extends Model {
function unbindModelAll() {
foreach(array(
'hasOne' => array_keys($this->hasOne),
'hasMany' => array_keys($this->hasMany),
'belongsTo' => array_keys($this->belongsTo),
'hasAndBelongsToMany' => array_keys($this->hasAndBelongsToMany)
) as $relation => $model) {
$this->unbindModel(array($relation => $model));
}
}
}
Aug 8: CakePHP SessionID regeneration
If you set the security level in a cake application to high, an algorithmus regenerates the SessionID.
Usually, this is not a bad idea. But as sometimes several requests are started simoultaniously, I discovered that random requests didn't get a proper user-id in the session and further found out that during AJAX-Requests requests the sessioncookie got deleted and in the same request a new one was set. As a result, a few AJAX-Requests failed due to an invalid session id.
After reducing the security to medium, the errors were gone.
It seems that this is a known issue to the developers (https://trac.cakephp.org/changeset/5982) and should have been fixed for over two years. As far as I can tell from the current source, SessionIDs are regenerated each tenth request - even if a XML-HTTP-Request was made. Due to race conditions this will make many developers headaches and is a bug that should be fixed.
Jul 17: CakePHP with PHP 5.3.x
Deprecated: Assigning the return value of new by reference is deprecated in /data/workspace/us/htdocs/tixi/cake/libs/debugger.php on line 99You can fix this by changing the default error_reporting value of the debug mode. Open the file
cake/libs/configure.phpAnd replace the line 295:
$ diff configure.php configure.php.new 295c295 < error_reporting(E_ALL); --- > error_reporting(E_ALL & ~E_DEPRICATED);
Mai 26: Did you know ...
Jul 23: Keep the indent small
Usually PHP-Code looks like this (source):
$dir = "/etc/php5/";
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
}
closedir($dh);
}
}
But IMHO this is bad coding style. A long indent does not make your code better. Far from it! On the one hand, an ident should be used to separate nested code blocks. On the other hand you should avoid unnecessary deep nesting. Try to alter your code and seperate error-cases in own blocks:
$dir = "/etc/php5/";
if(!is_dir($dir)) {
throw new Exception("...");
}
$dh = opendir($dir);
if(!$dh) {
throw new Exception("...");
}
while (($file = readdir($dh)) !== false) {
echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
}
closedir($dh);
This is kind of annoying if you have a class (+1 indent) and a method (+1 indent) and switch-block (+1 indent) in your code. If you now start to create further nested code, you have about 5-7 indents and it is hard to track your code. Not only because half of your screen is blank and you have to scroll vertically to see the rest of the line, sometimes the error-handling is several dozen lines behind the original statement that caused the error.
Dez 13: cakePHP Subnetcalc Helper
Ever needed some help when calculating a subnetmask or a broadcast address? Then this one is your friend!
lesen Sie mehrDez 13: PHP CSV to DB script
Neither phpmyadmin nor MySQL Query Browser is able to load a CSV file directly into a DB. As this is a frequent issue, i wrote an import script.
lesen Sie mehrOkt 12: Converting existing eclipse projects into PDT projects
In my workspace there are several projects, that were created before the PDT was released. During the PDT development there was an option in the project contextmenu to convert the project into a PDT project - but as it seems, this option was removed in the final version.
lesen Sie mehrSep 28: Useful php functions
You all now the implode() function. This function takes an array, and concatinates all values with a given string.
But sometimes you want to do the same thing with an array of arrays.
$data = array(
1 => array('name' => 'a'),
2 => array('name' => 'b'),
3 => array('name' => 'c')
);
In this case I want to implode all names in this array. So I wrote this function:
function implode_array($separator, $data, $element) {
if(!is_array($data)) {
trigger_error("parameter 2 must be an array", E_USER_ERROR);
}
$newData = array();
foreach($data as $key => $value) {
if(!isset($value[$element])) {
trigger_error("data[$key] has no element '$element'", E_USER_ERROR);
}
$newData[] = $value[$element];
}
return implode($separator, $newData);
}Usage:
echo implode_array(',', $data, 'name'); // a,b,c Sep 18: php suhosin pitfall
Regarding security, you often hear about hardened php or suhosin. Suhosin is a php security module, that offers a little protection against bugs in php (eg. buffer overflows).
I use this module and didn't find any problems ... until now
Suhosin has some extra protection against website-attackers. For example it limits the number of request-variables sent to the script by dropping any variables over a distinct limit. It took quite a while to figure that out
So of you have large forms with over 200 variables passed, you must alter your config to allow this kind of large requests.
Sep 7: Bank Account Validation
When using direct debit, it is helpful to know if the account really exists. The Bundesbank provides a list of valid bank agencies in Germany. To validate an account-number, there are more than 100 differenz validation methods. Way too much work to implement this for yourself.
But on Sourceforge I found a project named BAV (Bank Account Validator), that provides a (hopefully) full set of validation schemes for all bank agencies.
After downloading and extracting the files, you should check if your webserver has the requirements installed (doc/readme.txt).
BAV uses a local cache, to store the entire list of bank agencies. This cache is accessed via the PDO interface. So you can easily use a mySQL database.
Be aware that there is a bug in the BAV_DataBackend_PDO::install: classes/dataBackend/BAV_DataBackend_PDO.php:181 classes/dataBackend/BAV_DataBackend_PDO.php:197
The foreign keys didn't work for me mySQL 5.0.15 (InnoDB). You must create the foreign keys after creating the tables (ALTER TABLE), because they cross-reference each other. Or just comment them out as I did ![]()
After having a look at the example script (scripts/example.php) I wrote my own test-implementation.
Seems to work fine, great work Markus Malkusch!






PHP/MySQL



