FLOW3 action parameters and arrays of objects

PhpModel View-ControllerOrmTypo3 Flow

Php Problem Overview


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

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:

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

Php Solutions


Solution 1 - Php

/**
 * @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

Solution 2 - Php

remove the word array before $tags

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBenMorelView Question on Stackoverflow
Solution 1 - Phpuser2807024View Answer on Stackoverflow
Solution 2 - PhpShinto JosephView Answer on Stackoverflow