FLOW3操作参数和对象数组。

时间:2020-11-29 11:25:22

FLOW3 provides a convenient way to pass entities by ID in the URL, and get them automatically instantiated in the controller action's parameters:

FLOW3提供了一种方便的方法,通过ID在URL中传递实体,并在控制器动作的参数中自动实例化它们:

class PostController extends \TYPO3\FLOW3\MVC\Controller\ActionController {
    public function editAction(Post $post) {
        ...
    }
}

But what about the use case where you have checkboxes, each representing a particular object? It would be handy to get them autoinstantiated as well:

但是,如果您有一个复选框,每个复选框代表一个特定的对象,那么用例又如何呢?将它们自动实例化也很方便:

<input type="checkbox" name="tags[]" value="1" />
<input type="checkbox" name="tags[]" value="2" />
...

Is there a way to tell FLOW3 to auto-instantiate the $tags variable as an array of Tag objects? Something like:

是否有一种方法可以告诉FLOW3将$tags变量自动实例化为标记对象数组?喜欢的东西:

public function setTagsAction(Post $post, /** @var Model\Tag */ array $tags) {
    $post->setTags($tags);
}

2 个解决方案

#1


2  

/**
 * @param Post $post
 * @param \Doctrine\Common\Collections\ArrayCollection<\your\namespace\Model\Tag> $tag
 */

public function setTagsAction(Post $post, $tags) { ...

afaik Doctrine will convert your array to a Collection Holding Objects mapped by the provided array

afaik学说将把你的数组转换成一个集合,这个集合包含由所提供的数组映射的对象

#2


0  

remove the word array before $tags

在$标记之前删除单词数组

#1


2  

/**
 * @param Post $post
 * @param \Doctrine\Common\Collections\ArrayCollection<\your\namespace\Model\Tag> $tag
 */

public function setTagsAction(Post $post, $tags) { ...

afaik Doctrine will convert your array to a Collection Holding Objects mapped by the provided array

afaik学说将把你的数组转换成一个集合,这个集合包含由所提供的数组映射的对象

#2


0  

remove the word array before $tags

在$标记之前删除单词数组