更新hasone关系行为奇怪(cakephp) [英] Update hasone relation behaves strangely (cakephp)

查看:154
本文介绍了更新hasone关系行为奇怪(cakephp)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的展示 hasOne 画廊。
图库已存在,并将foreignKey showcase_id 设置为正确的值。
图库有一个文本字段,我尝试通过Showcase控制器更新。
我得到的结果是一个额外的画廊条目,沿原来的,而不是更新的原始条目。



我做错了什么? / p>

我的展示视图如下:


echo $ form-> create('Showcase',array('action'=>'update'));



echo $ form-> input('Showcase.id',array('type'=>'hidden','value'=> $ showcase ['Showcase'] ['id'])) ;



echo $ form-> input('Gallery.fulltext',array('type'=> ;'textarea','between'=>'< br>','value'=> $ showcase ['Gallery'] ['fulltext']));



echo $ form-> submit('提交文字');



echo $ form-> end();




Showcase-controller功能:


$ uses = array('Showcase','Gallery')



function update(){



if(!empty($ this-> data)){



$ this-> Showcase-> saveAll($ this-> data,array('validate'=>'first'));



}




展示模型 $ hasOne ='Gallery'和图库模型 $ belongsTo ='Showcase'



$ this-> Showcase-> saveAll / code>这里使用正确的函数?或者我可能需要更新图库控制器中的图库条目?

解决方案

我可以看到你的意思是不优雅,它会很好,如果它意识到你有一个hasOne关系,因此更新任何现有的记录。



但是目前在框架中我认为你最好的选择是添加 echo $ form-> input('Gallery.id',array ('type'=>'hidden','value'=> $ showcase ['Gallery'] ['id'])); b

作为旁注。在当前定义 $ showcase 的控制器动作中,如果您将它分配给 $ this-> data 将不需要总是指定'value'=> ...


I've got an existing Showcase that hasOne Gallery. The Gallery already exists with the foreignKey showcase_id set to the proper value. The Gallery has a text field that I try to update via the Showcase-controller. The result I get is an extra Gallery entry, along the original one, instead of an update of the original entry.

What am I doing wrong?

My Showcase-view looks as follows:

echo $form->create('Showcase', array('action'=>'update'));

echo $form->input('Showcase.id', array('type'=>'hidden', 'value'=>$showcase['Showcase']['id']));

echo $form->input('Gallery.fulltext', array('type'=>'textarea', 'between'=>'<br>', 'value'=>$showcase['Gallery']['fulltext']));

echo $form->submit('Submit text');

echo $form->end();

My Showcase-controller function:

$uses = array('Showcase','Gallery')

function update(){

if(!empty($this->data)){

$this->Showcase->saveAll($this->data, array('validate'=>'first'));

}

}

The Showcase model $hasOne = 'Gallery' and the Gallery model $belongsTo = 'Showcase'.

Is $this->Showcase->saveAll() the proper function to use here? Or do I maybe need to update the Gallery entry within the Gallery controller? That will probably work but is seems so un-elegant.

解决方案

I can see what you mean by being un-elegant, it would be nice if it realised you had a hasOne relationship and therefore updated any existing record.

However currently within the framework I think your best option would be to add echo $form->input('Gallery.id', array('type'=>'hidden', 'value'=>$showcase['Gallery']['id'])); to your view.

As a side note. In the controller action that is currently defining $showcase if you instead assign it to $this->data then you won't need to always specify 'value' => ... in your form inputs.

这篇关于更新hasone关系行为奇怪(cakephp)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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