I have two Models that I want to connect by a hasMany relationship.


   - Model 1 is a *School* -- basically just text info like school name, 
   address.  I can independently add/edit/view/delete schools OK.
   - Model 2 is a *File*.  I can independently add/edit/view/delete files 
   OK.  File are saved to MySQL via this 
technique<http://cakebaker.42dh.com/2006/04/15/file-upload-with-cakephp/>
   .

I want Schools to have many files -- so in addition to textual data, people 
can upload any number of photos of the school.

   - I added to my School model:  var $hasMany = 
   array('AttachedFile'=>array('className'=>'AttachedFile'));
   - I added to my AttachedFile model:  var $belongsTo = 
   array('School'=>array('className'=>'School'));

*First*, how would I render the add form for adding a School to include the 
option to select a file?  (It would be nice if they could add a file, then 
another "add file" option came up.  I don't know how to do that, so for now 
I am OK making them to add files one at a time - e.g. add school, add file 
1, save, then edit school, add file 2, save, edit school, add file 3, etc.)

School's *add.ctp*:

echo $form->create('School', array('action'=>'add'));

echo $form->input('School.district', array('label'=>'School District'));

echo $form->input('School.type');

// neither of these work:

echo $form->file('AttachedFile.File', array('label'=>'File', 'action' => 
'add', 'type' => 'file'));

echo $form->create('AttachedFile', array('action' => 'add', 'type' => 
'file'));

Inside school_controller's add, I'm not getting the actual file.

if (!empty($this->data)) {

           // use saveAll to save the hasMany relationship

           if ($this->School->saveAll()) {

                $this->Session->setFlash('School data saved.');

                $this->redirect(array('action' => 'index'));

            }


This is what I'm seeing passed to the add function:
 

Array
(
    [School] => Array
        (
            [district] => 
            [trc] => 
            [school_code] => 
            [type] => 
            [sn] => 
            [primary_school] => 
        )

    [AttachedFile] => Array
        (
            [File] => 250px-Computer-aj_aj_ashton_01.png
        )

)

Here is a functioning add from attached_file_controller -- this works 
independently.

function add() { 

        //echo ("<pre>"); print_r($this->data); echo("</pre>");

        exit;

        if (!empty($this->data) && 

            
 is_uploaded_file($this->data['AttachedFile']['File']['tmp_name'])) {

            $fileData = 
fread(fopen($this->data['AttachedFile']['File']['tmp_name'], "r"), 

                                    
 $this->data['AttachedFile']['File']['size']);

            

            $this->data['AttachedFile']['name'] = 
$this->data['AttachedFile']['File']['name'];

            $this->data['AttachedFile']['type'] = 
$this->data['AttachedFile']['File']['type'];

            $this->data['AttachedFile']['size'] = 
$this->data['AttachedFile']['File']['size'];

            $this->data['AttachedFile']['data'] = $fileData;

            $this->AttachedFile->save($this->data);


            $this->redirect('index');

        }

    }

-- 
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
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to