How to update cakephp entry

this is the code to edit

function edit($id){

    if(!empty($this->data)) {
        if($this->Article->save($this->data)) {
            pr($this->Article);
            $this->Session->setFlash("Article Saved!");
            $this->redirect('/articles/view');        
            }    
    }
    else    
        $this->data = $this->Article->findByArticleId($id);

}

and for presentation

<?php 
echo $form->create('Article', array('type' => 'post','action'=>'edit')); 
?>

<table>
<tr><td>Edit Article</td></tr>
<tr><td>Title: </td><td><?php echo $form->text('article_title',array('label'=>false,'type'=>'text','size'=>50)); ?></td></tr>
<tr><td>Text: </td><td><?php echo $form->textarea('article_text',array('label'=>false,'class'=>'ckeditor','columns'=>100,'row'=>8,'width'=>100));?></td></tr>
<tr><td>Published: </td><td><?php echo $form->radio('article_published',array('1'=>'Yes','0'=>'No'),array('legend'=>false));?></td></tr>
</table>
<?php echo $form->end('Save'); ?>

the problem is that it always adds a new record instead of updating the current record. how can i fix this?

+3
source share
1 answer

The reason the new record is inserted is because the CakePHP method does not know the identifier of the record you are editing. In principle, when the data passed to the method save()does not contain the record identifier, or the identifier does not exist in the table, it save()will insert a new record instead of updating the existing one.

echo $form->input('id'); . . , , , .

+10

Source: https://habr.com/ru/post/1762168/


All Articles