background preloader

CakePHP

Facebook Twitter

File uploading, file storage and CakePHPs MediaView class by Florian Krämer, Cake Development Corporation. This article includes how to upload and store files, because I've seen a lot of discussion about that too, but if you're just interested in how to use the MediaView class scroll down. Handling file uploads in CakePHP First let's start with the required form, to create a file upload form all you have to do is this: Show Plain Text echo $form->create('Media', array('action' => 'upload', 'type' => 'file'));echo $form->file('file');echo $form->submit(__('Upload', true)); The "type" in the options of Form::create() takes post, get or file.

To configure the form for file uploading it has to be set to file which will render the form as a multipart/form-data form. When you submit the form now, you'll get data like this in $this->data of your controller: Array( [Media] => Array ( [file] => Array ( [name] => cake.jpg [type] => image/jpeg [tmp_name] => /tmp/hp1083.tmp [error] => 0 [size] => 24530 ) )) Ok, now the big question with a simple answer is where the file data should be processed, guess where.

Pagination, deep sort, and HABTM. Here’s a problem that had me scratching my head for hours. Creating views with HABTM relationships should be a bread-and-butter task in any web application. So you’d think Cakephp has made the workflow as intuitive as possible, but what a headache. The standard method of HABTM queries in the documentation seems intuitive enough at first glance.

And this is the approach I’ve used for a while. $this->Recipe->bindModel(array('hasOne' => array('RecipesTag'))); $this->Recipe->find('all', array( 'fields' => array('Recipe.*'), 'conditions'=>array('RecipesTag.tag_id'=>124) // id of Dessert )); That is until I needed to be able to sort the resulting table by the deeper associations with pagination. In which case something like: $paginator->sort('Condition','Disorder.condition_id'); Where Disorder is a deep associated table two levels down, is going to return the error that the column Disorder.condition cannot be found. The recommendation is to use on-the-fly associations. Here’s some code: Php - Unit testing in CakePHP. Creating PDF files with CakePHP and TCPDF. Automatically choose database connections in CakePHP | Edward A. Webb (.com) <div class="greet_block wpgb_cornered wpgb_shadowed"><div class="greet_text"><div class="greet_image"><a href=" rel="nofollow"><img src=" alt="WP Greet Box icon"/></a></div>Hello there!

If you are new here, you might want to <a href=" rel="nofollow"><strong>subscribe to the RSS feed</strong></a> for updates on this topic. <div style="clear:both"></div></div></div> When developing applications it is wise to isolate development environments from live production environments. This is true of your code and your database structures For me this means my local SuSE server runs any developmental code before I push it off to a shared hosting account out on the real live web. The frustrating part for me was changing between two distinct database connections. Well no more! 1. 2. 3. 1. 2. 3.