phpcakephp

cakephp no id passed to Form


I have two forms, one to edit room details and the other to edit Extras. Within the forms I pull in a file upload and pass in the id. For some reason one requires a url and the id does not get passed in. Both have the same code. Can see why they are differnt.

Room form

            <div class="boxgrid grid_8">
            <?php echo $this->element('attachments',array('control'=>'upgrades','id'=>$this->data['AddOn']['id'],'att'=>$this->data['Attachment'])); ?>
            </div>

Room upload

Form->create('Room', array('type' => 'file'));?>

    <legend><?php __('Upload Room Images'); ?></legend>
<?php
    echo $this->Form->input('id');
?>
<input type="hidden" name="data[Attachment][0][model]" value="Room" id="Attachment0Model" />
<input type="hidden" name="data[Attachment][0][group]" value="attachment" id="Attachment0Group" />
<div class="input file required"><input type="file" name="data[Attachment][0][file]" class="" id="Attachment0File" /></div>


<div class="submit"><button>Upload</button></div>
<div>Upload files</div>

Extras Form

            <div class="boxgrid grid_8">
            <?php echo $this->element('attachments',array('control'=>'upgrades','id'=>$this->data['AddOn']['id'],'att'=>$this->data['Attachment'])); ?>
            </div>

Extras Upload

Form->create('Upgrade', array('type' => 'file','url'=>'/admin/upgrades/addfiles','id'=>'AddOnAdminAddfilesForm'));?>

    <legend><?php __('Upload Addon Images'); ?></legend>
<?php
    echo $this->Form->input('id');
?>
<input type="hidden" name="data[Attachment][0][model]" value="AddOn" id="Attachment0Model" />
<input type="hidden" name="data[Attachment][0][group]" value="attachment" id="Attachment0Group" />
<div class="input file required"><input type="file" name="data[Attachment][0][file]" class="" id="Attachment0File" /></div>


<div class="submit"><button>Upload</button></div>
<div>Upload files</div>

Javascript on each form:

<script type="text/javascript">
  $(document).ready(function() {

    $("div#uploader").resloader();
    $("div#uploader").load('<?=BASE_URL?>/admin/upgrades/addfiles/<?=$this->data['AddOn']['id']?>',null,function(){}).fadeIn();

Upgrades Contoller

            function admin_addfiles($id = null) {
            $this->layout = null;

                if (!$id && empty($this->data)) {
                    $this->Session->setFlash(__('Invalid Add On', true));
                    $this->redirect(array('controller' => 'upgrades',  'action' => 'index'));
                }

                if (!empty($this->data)) {
                    $this->layout = null;
                    //if(empty($this->data['AddOn']['id'])){unset($this->data['AddOn']);}

                    // restructure data for uploader plugin // NEED TO GET RID OF THIS ? MOVE IT
                    $tmp_file = $this->data['Attachment'][0]['file'];
                    $tmp_file['extension'] =  array_reverse(explode('.', $tmp_file['name']));
                    $tmp_file['extension'] = $tmp_file['extension'][0];
                    $tmp_file['title'] = strtolower(substr($tmp_file['name'],0,(0-strlen('.'.$tmp_file['extension']))));
                    $this->data['Attachment'][0]['alternative'] = ucwords(str_replace('_',' ', $tmp_file['title']));

                    if ($this->AddOn->saveAll($this->data, array('validate' => 'first'))) { 

                        $id = $this->AddOn->Attachment->getLastInsertID();
                        $att = $this->AddOn->Attachment->query("SELECT * from attachments WHERE id = ".$id);
                        $this->set('attachment',$att[0]['attachments']);

                    } else {
                        $tmp_file['name'] = 'INVALID FILE TYPE';
                    }


                    //debug($this->data);
                    $this->set('file', $tmp_file);
                    $this->RequestHandler->renderAs($this, 'ajax');
                    $this->render('../elements/ajax');

                }

                if (empty($this->data)) {
                    $this->data = $this->AddOn->read(null, $id);
                }

            }

        }

Solution

  • Your problem is with the $this->data. Check how it is filled in your controller.

    Both views are NOT the same, the main difference is in the create form.

    Form->create('Room', array('type' => 'file'));?>

    Form->create('Upgrade', array('type' => 'file','url'=>'/admin/upgrades/addfiles','id'=>'AddOnAdminAddfilesForm'));?>

    As you can see, one has the first parameter 'Room' and the other one is 'Upgrade', this IS important since you call the id like this

    echo $this->Form->input('id');

    Cake expects that for the first case you have something like, $this->data['Room']['id'] and the second one $this->data['Upgrade']['id']

    If you pass from the controller your id variable like this

    $this->set('id',$id);
    

    then in the view you can do somthing like this

    <?php
        echo $this->Form->input('id', array('value'=>$id, 'type'=>'hidden'));
    ?>
    

    Hope this solves your answer, if not, please post the $this->data value of each and the part of the controller where you assign $this->data