仅更新cakephp中的特定字段

时间:2014-04-10 14:15:08

标签: cakephp cakephp-2.3

我有一个小问题。我有一个表 item_pictures ,有6个字段 img1 | img2 | img3 | ima4 | IMG5 |存储5张图片。所以它意味着一行Batabases有5个字段存储图像的路径。我想做的是在每个图像旁边创建一种按钮,当用户点击它时,它只是更新图像的字段使其变空,这就是我想要的。所以我只想更新或更改一个特定字段。是可能的,如果是这样的话?

到目前为止我已经纪念过的代码:

$this->Form->postLink($this->Html->tag('i', '', 
                array('class' => 'glyphicon glyphicon-remove')),
                array('controller' => 'item_pictures','action' => 'delete_current_img', $item['Item']['id'] .'-'. 'img'.$i .'-'. $pictures['img'.$i]) ,
                array('escape' => false), __('Are you sure you want to delete This Image?'))

所以我希望在控制器中获取此变量并使用id =>更新行 $ item ['Item'] ['id'] ,在列 img。$ i 上,其值为 $ pictures ['img'。$ i] ,并将其设为空...

1 个答案:

答案 0 :(得分:1)

我认为你非常接近,所以我会用

将$ i = 1循环到$ i = 5
$item_picture_id = $item['Item']['ItemPicture']['id'] //or something, im not sure how you have done your relations

echo $this->Form->postLink($this->Html->tag('i', '',
    array('class' => 'glyphicon glyphicon-remove')),
    array('controller' => 'item_pictures','action' => 'delete_current_img', $item_picture_id, 'img'.$i) ,
    array('escape' => false), __('Are you sure you want to delete This Image?'));

因此,例如图像5($ i = 5)应解析为URL:item_pictures / delete_current_img / ID / img5 /

然后你的控制器就有(非常简单的例子):

public function delete_current_img($id,$field){
    $this->ItemPicture->id = $id;
    $this->ItemPicture->saveField($field,'');
    //then just go back to where you come from
    $this->redirect(array('controller' => 'foo', 'action' => 'where_ever_the_delete_came_from'));
}

希望它有所帮助!