cakephp $this->data loses a LOT of its data array after form submit

一笑奈何 提交于 2020-01-06 18:12:18

问题


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

and my controller code:

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

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?


回答1:


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 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. A GET request: let's load the data from the database.

  2. A POST request: let's save the data,

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

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.

Try something like this:

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(), part of CakePHP's library, will do the job of merging the submitted and complete data together.




回答2:


Have you tried using cookies to store post variables into in-case the page gets refreshed or an error occurs?



来源:https://stackoverflow.com/questions/9690300/cakephp-this-data-loses-a-lot-of-its-data-array-after-form-submit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!