简体   繁体   English

将实体对象作为参数传递给CakePHP 3.x中的控制器功能

[英]Pass Entity object as parameter to controller function in 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. 我有一个CakePHP 3.1项目,我正在寻找一种使用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 rawurlencode()期望参数1为字符串

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. 任何帮助或更好的替代方法表示赞赏。

Try to pass only the entity id as parameter (not the entity) and do the job inside unlink action: 尝试仅将实体ID作为参数(而不是实体)作为参数传递,并在取消链接操作中执行以下操作:

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());       
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM