dsbgltg159136540 2016-06-03 14:42
浏览 63

将Entity对象作为参数传递给CakePHP 3.x中的控制器函数

I have a project of CakePHP 3.1 and I'm looking for a way to pass as parameter an Entity object and an array to a controller using the FormHelper postLink.

What I have already tried:

echo $this->Form->postLink(
                __('Unlink'),
                ['action' => 'unlink', $entity, $array],
                ['confirm' => __('Are you sure to unlink {0}?', $array['name'])]);  

My controller:

public function unlink($entity,$array)
{
    $theTable = TableRegistry::get('tableName');
    $theTable->Model->unlink($entity, $array);
    return $this->redirect($this->referer());
}

This is displaying a pretty known error:

rawurlencode() expects parameter 1 to be string

But I haven't find any post that fits my needs, I guess it is because I need to pass an object instead of basic types.

Any help or better alternative is appreciated.

  • 写回答

1条回答 默认 最新

  • dparivln22034 2016-06-03 18:11
    关注

    Try to pass only the entity id as parameter (not the entity) and do the job inside unlink action:

    echo $this->Form->postLink(
                __('Unlink'),
                ['action' => 'unlink', $entity->id, $array],
                ['confirm' => __('Are you sure to unlink {0}?', $array['name'])]);
    

    and inside unlink:

    public function unlink($id = null ,$array = [])
    {
        if (is_null($id)) {
            // an error flash message can be set here
            return $this->redirect($this->referer());
        }       
        $theTable = TableRegistry::get('tableName');
        $theEntity = $theTable->get($id);
        $theTable->unlink($theEntity, $array);
        return $this->redirect($this->referer());       
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画