cakephp $ this->数据在提交表单后丢失其数据数组的LOT [英] cakephp $this->data loses a LOT of its data array after form submit

查看:247
本文介绍了cakephp $ this->数据在提交表单后丢失其数据数组的LOT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个manage_photos页面的$ this->数据包含大量有关其单位的信息。我有一个窗体,在数据库中进行适当的更改。然而,在提交表单时,$ this-> data(如使用pr($ this-> data)时可见)在页面刷新后丢失其数组数据的MOST。这是我的视图中的表单代码:

I have a manage_photos page whose $this->data contains a great deal of information about its Unit. I have a form which makes the appropriate changes in the database. However, upon submission of the form, $this->data (as seen when using pr($this->data)) loses MOST of its array data once the page refreshes. Here is the form code in my view:

echo $this->Form->create('Unit', array(
    'action' => 'manage_photos',
    'type' => 'file',
    'inputDefaults' => array(
        'label' => false,
        'div' => false
        )
    )
);
echo $this->Form->hidden('id');
$count=0;
foreach($this->data['Image'] as $img) {
echo '<div class="grid_4 manage-pics">';
echo $this->Form->hidden('Image.'.$count.'.id');
$char_list='http';
$link=strpos($img['img'], $char_list);
if($link===false) {
    echo '<img src="/img/uploaded_img/user/';
    echo $this->data['User']['id'];
    echo "/";
    echo $img['img'];
    echo '" alt=" "  />';
    }
elseif($link!==false) {
    echo '<img src="';
    echo $img['img'];
    echo '" alt="" />';
}
echo '<h4>Picture:  '.$img['img'].'</h4>';
echo '<br />';
echo $this->Form->input('Image.'.$count.'.img_alt', array('label'=>'A description of this picture', 'div'=>true, 'size'=>45));

$count++;
echo '</div>';
}
echo $this->Form->end('Update Photos');?>
<?php echo $this->Session->flash(); ?>

和我的控制器代码:

function manage_photos($id) {
    $this->set('title', 'Edit your photos');
    $this->Unit->id = $id;    
    if (empty($this->data)) {        
        $this->data = $this->Unit->read();    
    } else { 
        if ($this->Unit->saveAll($this->data)) {            
            $this->Session->setFlash('Your photos have been updated.',  'success');            
        }   
    }
}

数组中的模型在我进行编辑时改变了,但是有没有办法强制蛋糕返回原来的$ this->数据?当页面刷新时,我丢失了所有的图像src。也许我不应该做一个页面刷新,或者我需要实际上坚持某种重定向回到控制器中的同一页面?

I assume it is only returning the models in the array that were changed when I made edits, but is there any way to force cake to return the original $this->data? I lose all of my image src when the page refreshes. Perhaps I shouldn't be doing a page refresh, or do I need to actually stick some sort of redirect back to this same page in the controller?

推荐答案

Web和HTTP协议是无状态的。这意味着在Web编程中,默认情况下,请求之间不保留数据。实际上,CakePHP默认情况下启用了会话,这在许多情况下可以帮助解决这个问题,但是在这里我们可能需要不同的东西。

The web and the HTTP protocol are stateless. This means that in web programming, by default, there is no data preserved between requests. Actually CakePHP has sessions enabled by default which can help get around this problem in many circumstances, but here we probably need something different.

$ this- > data 只能从页面上的任何表单域填充。这意味着在您的控制器中有几种情况可以处理:

$this->data is only populated from whatever form fields you have on the page. This means in your controller you have several cases to deal with:


  1. GET请求:让我们从数据库加载数据。

  1. A GET request: let's load the data from the database.

POST请求:让我们保存数据

A POST request: let's save the data,


$ b b

a)成功:重定向到另一个页面或重新加载所有数据。

a) Successfully: either redirect to another page or load all the data afresh.

b)不成功:让我们加载数据库中的所有数据,与我们提交的(无效)数据以再次显示表单。如果我们不执行合并操作,用户必须重新输入很少需要的所有更改。

b) Unsuccessfully: let's load all the data from the database and merge it with our submitted (invalid) data for displaying the form again. If we don't do the merge thing the user has to retype all changes which is rarely desirable.

尝试这样:

function manage_photos($id) {
    $this->set('title', 'Edit your photos');
    $this->Unit->id = $id;
    if (empty($this->data)) {
        // 1. GET request
        $this->data = $this->Unit->read();
    } else {
        if ($this->Unit->saveAll($this->data)) {
            // 2a POST successful
            $this->Session->setFlash('Your photos have been updated.',  'success');
            $this->data = $this->Unit->read();
        } else {
            // 2b POST unsuccessful
            Set::merge($this->Unit->findById($id), $this->data);
        }
    }
}

Set :: merge()是CakePHP库的一部分,它将完成将提交和完整数据合并在一起的工作。

Set::merge(), part of CakePHP's library, will do the job of merging the submitted and complete data together.

这篇关于cakephp $ this-&gt;数据在提交表单后丢失其数据数组的LOT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆