MY QUESTION
What the difference between those two lines in a form request/POST?
- $article->getComments() as $comment
- $form->get('Comments')->getData() as $comment
CONTEXT
ENTITY ARTICLE
class Article
{
private Comments
public function __construct() {
$this->Comments = new\Doctrine\Common\Collections\ArrayCollection()
}
public function getComments()
{
return $this->comments
}
FORM REQUEST
$article is an object with some comments.
$form = $this->createForm(new ArticleType(), $article);
$request = $this->getRequest();
if($request->getMethode() == 'POST'){
$form->bind($request);
if($form->isValid()) {
$liste_comments = array();
$liste_comments_bis = array();
foreach($article->getComments() as $comment){
$liste_comments[] = $comment
}
foreach($form->get('comments')->getData() as $comment_bis){
$liste_comments_bis[] = $comment_bis
}
}
}
ARTICLETYPE add article contenu and add a collection of comments
Deep inside all the work done by
$form->bind($request)
, Symfony's form system reads all of the data from the HTTP requests and assigns it to the form's underlying data object. So, when you run$form->getData()
it's returning the form's default values merged with values found in the request.Let's walk through it
Does that help?