cakephp没有id传递给Form

时间:2011-09-14 20:02:01

标签: cakephp

我有两个表单,一个用于编辑房间详细信息,另一个用于编辑附加功能。在表单中,我拉入文件上传并传入id。由于某种原因,一个人需要一个url并且id不会被传入。两者都有相同的代码。可以看出他们为什么不同。

会议室表格

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

会议室上传

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

    <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>

附加表格

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

其他上传

表单 - &gt;创建('升级',数组('type'=&gt;'文件','url'=&gt;'/ admin / upgrades / addfiles','id'=&gt;'AddOnAdminAddfilesForm') );?&GT;

    <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:

<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();

升级控制器

            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);
                }

            }

        }

1 个答案:

答案 0 :(得分:1)

您的问题在于$ this-&gt;数据。检查控制器中的填充方式。

两种观点都不一样,主要区别在于创作形式。

  

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

     

表单 - &gt;创建('升级',数组('type'=&gt;'文件','url'=&gt;'/ admin / upgrades / addfiles','id'=&gt;'AddOnAdminAddfilesForm') );?&GT;

正如你所看到的,第一个参数是'Room'而另一个是'Upgrade',这很重要,因为你这样称呼id

  

echo $ this-&gt; Form-&gt; input('id');

Cake预计,对于第一种情况,你有类似的东西,例如$ this-&gt; data ['Room'] ['id'],第二种情况是$ this-&gt; data ['Upgrade'] ['id' ]

如果你从控制器传递你的id变量

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

然后在视图中你可以做这样的事情

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

希望这能解决您的答案,如果没有,请发布每个和控制器部分的$ this-&gt;数据值,您指定$ this-&gt; data