Hi elogic,

since I did the CSV Import for a customer, I can not provide any code 
examples, so I'm trying to explain the process a little more detailed. I can 
not guarantee that the following is absolutely MVC conform or that there are 
no better ways to do this!

Assumption: A Customer wants to import his employee data into an already 
existing application. In the application we have got an employee model, we 
are going to use for this.

For avoiding code repitition, we will use a behavior being able to handle 
uploads and imports. To create a behavior, the following chapter from the 
cakebook should be read: http://book.cakephp.org/view/1071/Behaviors
Additionally, you can look at one of the behaviors from the bakery.

The upload then can be started from any view, by creating a form of type 
'file':

<?php echo $this->Form->create('Model', array('action' => 
'add/create/whatever', 'type' => 'file', 'name' => 'name')); ?>
// Some fields, like caption for an Image or other stuff. Whatever you like 
;-)
<?php echo $this->Form->end(__('Submit', true));?>

On clicking the submit Button, the action of this form is invoked. That is 
also the part, we need to handle the upload ourselfs. The Fileinformation 
are stored in our submitted array, called 'name'. There are two php methods 
we need for our behavior first:

*is_uploaded_file* ( string $filename )  ==> 
http://de3.php.net/manual/de/function.is-uploaded-file.php
*move_uploaded_file* ( string $filename , string $destination ) ==> 
http://de3.php.net/manual/de/function.move-uploaded-file.php

You just use is_uploaded_file to check, and in case its true, you move the 
uploaded file to your designated location.

But since you got your array ready and you are using that little comfortable 
Framework, you could also use the Cakephp-native FileClass for this: 
http://api13.cakephp.org/class/file
In that case, you would create a file, check for existence and move it to 
your preferred destination. Don't forget to check the filename and 
eventually rename it, so no other file gets overwritten.

We now got our CSV File on the server and are ready to import that stuff.

The way to start your import is totally free of choice. You can either poll 
a directory for new files and start an import if so, or you could start it 
manually. The steps needed for import stay the same.
1. Create a new File Object
2. Open it and read the complete content

-- Here might start a for loop --

3. Use a function like *str_getcsv *to parse a single line
4. Create a new Object from your Model
5. Assign the fields from your Array
6. Save the Object to your database

-- Here might end a for loop --

These are the basic steps - at least if I haven't forgotten some - and I 
hope it helps. Anyways, you should read the Cakebook first. It's okay, to 
glance through the chapters and read them afterwards, when really needed, 
but you should know what actually is possible and how it can be achieved.

Kind regards,
Vulinux

-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to