saveAll()插入新行而不是更新

编程入门 行业动态 更新时间:2024-10-19 23:47:44
本文介绍了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()插入新行而不是更新

本文发布于:2023-10-26 23:41:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531684.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不是   saveAll

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!