saveAll()插入新行而不是更新 [英] saveAll() inserts new row instead of updating

查看:1077
本文介绍了saveAll()插入新行而不是更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用CakePHP SaveAll()方法更新记录,但是它添加了新行而不是更新它。

I'm trying to update a record with CakePHP SaveAll() method but however, it adds new row instead of updating it.

我的模型如下:商店hasOne地图

My model are as follow: A Store hasOne Map

class Store extends AppModel {  
    var $name = 'Store';

    var $hasOne = array(
        'Map' => array(  
            'className' => 'Map',
            'foreignKey' => 'store_id',
            'dependent' => true,
            'exclusive' => true
        )
    );
}

我的编辑表单有一个商店ID(隐藏字段)告诉CakePHP更新

My edit form has a Store ID (hidden field) to tell CakePHP to update a particular record only.

<?php echo $this->Form->input('Store.id'); ?>
<?php echo $this->Form->input('Store.name', array('label' => 'Store name', 'required')); ?>
<?php echo $this->Form->input('Map.latlng', array('label' => 'Map location', 'required')); ?>

我在商店控制器中的编辑方法如下。

My edit method in the Store controller are as follow.

if ($this->Store->saveAll($this->data)) {
    $this->Session->setFlash('Store has been updated.');
    $this->redirect(array('controller' => 'store', 'action' => 'admin_index'));
}

每当我编辑商店时,商店名称都会更新,但CakePHP会继续在地图表上插入新行。

Whenever I edit a Store, the store name is updated fine but CakePHP keeps on inserting a new row on the Map table.

我在这里缺少什么?感谢

Am I missing anything here? Thanks

其他信息

我的调试如下

Array
(
    [Store] => Array
        (
            [id] => 52
            [name] => Sena The Accessorizer
        )

    [Map] => Array
        (
            [latlng] => 3.1580681, 101.7126311
        )

)


推荐答案

正如@Dunhamzzz指出,地图没有ID,因此CakePHP插入了一条新记录。

As @Dunhamzzz has pointed out, Map does not have an ID, hence CakePHP inserts a new record.

解决这个问题,我为 Map.id

<?php echo $this->Form->input('Map.id'); ?>

这将告诉CakePHP更新该特定记录,而不是插入一个新记录。

This will tell CakePHP to update that particular record instead of inserting a new one.

这篇关于saveAll()插入新行而不是更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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