This blog is about the PHP solutions I have to think of when I'm developing systems almost every single day...

Thursday, February 21, 2008

Fixup for the attach to anything plugin for cakephp

There is a great plugin for cakephp. It basically allows you to attach files to any models that you have. Quite useful if you have many models which needs attachments. It is at http://bakery.cakephp.org/articles/view/attachments .

This is a great piece of code. Got so much features already built in. Hats off to the developers and all the contributors.

But there is a few things I would like to mention here on how to get it working. It took me a whole day just to figure it out.

The first part is the database. The script included in the download is missing a the auto increment extra. So you should change the file in config/sql/attachments.php so that the line with:


'id' => array('type'=>'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'),


is changed to:

'id' => array('type'=>'integer', 'null' => false, 'default' => NULL, 'key' => 'primary', 'extra' => 'auto_increment'),


Note the auto increment part at the end. Once that is done you can create it with bake. The command is just (running from your current cake root directory):
cake/console/cake schema run create attachments

That should set up your database if all your configuration is right.

And then I find that I need to change the file models/behaviors/attachment.php. In there under the function setup I have to add:

if(!is_array($config)){
$config=$this->defaultSettings;
}

Right at the beginning of the function so that there would be a default if there wasn't one passed by the user (And actually I don't have a clue how to pass one actually).

And then in the same file under the beforeSave function I have to add the following code:

foreach($attachment as $key=>$data){
if(is_array($data)){
foreach($data as $skey=>$sdata){
$uh[$skey][$key]=$sdata;
}
}
}
$attachment=$uh;


before line 365 just before the test:

if(isset($attachment['file'])) {


because it seems the arrangments of the array is wrong for the Transfer object. But apart from that it works pretty well.

0 comments:

About Me

My Photo
Abdullah Zainul Abidin
I am what I am because of what I am...
View my complete profile