我的问题
表单request / POST中这两行之间有什么区别?
背景
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是一个带有一些注释的对象。
$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 添加文章contenu并添加一组评论
答案 0 :(得分:0)
在$form->bind($request)
完成的所有工作中,Symfony的表单系统从HTTP请求中读取所有数据并将其分配给表单的基础数据对象。因此,当您运行$form->getData()
时,它会返回表单的默认值,并与请求中的值合并。
让我们来看看
// Create a form object based on the ArticleType with default data of $article
$form = $this->createForm(new ArticleType(), $article);
// Walk over data in the requests and bind values to properties of the
// underlying data. Note: they are NOT bound to $article - $article just
// provides the defaults/initial state
$form->bind($request);
// Get the Article entity with bound form data
$updatedArticle = $form->getData();
// This loops over the default comments
foreach ($article->getComments() as $comment) {}
// This loops over the submitted comments
foreach ($updatedArticle ->getComments() as $comment) {}
// For the purpose of my example, these two lines return the same thing
$form->get('comments')->getData();
$form->getData()->getComments();
这有帮助吗?