cakephp 没有将 id 传递到表单

问题描述 投票:0回答:1

我有两种表格,一种用于编辑房间详细信息,另一种用于编辑附加信息。在表单中,我提取文件上传并传递 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->create('房间', array('type' => '文件'));?>

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

额外上传

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:

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

            }

        }
php cakephp
1个回答
1
投票

您的问题出在 $this->data 上。检查它是如何填充到控制器中的。

两种视图并不相同,主要区别在于创建形式。

Form->create('房间', array('type' => '文件'));?>

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

正如你所看到的,一个有第一个参数“Room”,另一个是“Upgrade”,这很重要,因为你这样调用id

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

Cake 期望对于第一种情况,您有类似 $this->data['Room']['id'] 的内容,而第二种情况是 $this->data['Upgrade']['id']

如果您从控制器传递您的 id 变量,如下所示

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

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

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

希望这能解决您的问题,如果没有,请发布每个的 $this->data 值以及您分配 $this->data 的控制器部分

© www.soinside.com 2019 - 2024. All rights reserved.