symfony2 - 尝试保存一对多/多对一使用的表单数据时出错

时间:2012-05-16 18:28:32

标签: symfony doctrine-orm symfony-forms

我有一份提交文章的表格。该表单有一个类别多选框,使用我的Category.php实体从DB中提取类别。表单生成正常,但在尝试提交时出现以下错误:

Property "categories" is not public in class "Natknow\EditorBundle\Entity\Article". Maybe you should create the method "setCategories()"?

模板表单代码:

[...]
<div id="artFormG1">
    <?php echo $view['form']->row($form['title']) ?>
    <?php echo $view['form']->row($form['description']) ?>
    <?php echo $view['form']->row($form['source']) ?>
</div>

<div id="artFormG2">
    <?php echo $view['form']->row($form['categories']) ?>
</div>

<?php echo $view['form']->row($form['body']) ?>
<br />
<input class="button" type="submit" formnovalidate = "true" />

<?php echo $view['form']->rest($form) ?>
[...]

ArticleType.php - 表单类

[...]
    $builder->add('title', 'text', array(
        'attr' => array('class' => 'artFormLeft')
    ));
    $builder->add('description', 'text', array(
        'attr' => array('class' => 'artFormLeft')
    ));
    $builder->add('source', 'text', array(
        'attr' => array('class' => 'artFormLeft')
    ));
    $builder->add('categories', 'entity',
        array('class' => 'NatknowEditorBundle:Category',
            'property' => 'name',
            'expanded' => false,
            'multiple' => true,
        )
    );
    $builder->add('body', 'textarea', array(
        'attr' => array('class' => 'tinymce'),
    ));
[...]

Article.php - 文章表实体

[...]

/**
 * Add categories
 *
 * @param Natknow\EditorBundle\Entity\ArticleCategory $categories
 */
public function addArticleCategory(\Natknow\EditorBundle\Entity\ArticleCategory $categories)
{
    $this->categories[] = $categories;
}

/**
 * Get categories
 *
 * @return Doctrine\Common\Collections\Collection 
 */
public function getCategories()
{
    return $this->categories;
}
[...]

ArticleCatigory.php - article_category表实体

[...]
/**
 *  @ORM\Id
 *  @ORM\Column(type="integer")
 *  @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;

/**
 *  @ORM\ManyToOne(targetEntity="Article", inversedBy="categories", cascade={"all"})
*/
protected $article;

/**
 *  @ORM\ManyToOne(targetEntity="Category", inversedBy="articles", cascade={"all"})
*/
protected $category;

/**
 *  @ORM\Column(type="smallint")
*/
protected $isParent = 0;

/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * Set isParent
 *
 * @param smallint $isParent
 */
public function setIsParent($isParent)
{
    $this->isParent = $isParent;
}

/**
 * Get isParent
 *
 * @return smallint 
 */
public function getIsParent()
{
    return $this->isParent;
}

/**
 * Set article
 *
 * @param Natknow\EditorBundle\Entity\Article $article
 */
public function setArticle(\Natknow\EditorBundle\Entity\Article $article)
{
    $this->article = $article;
}

/**
 * Get article
 *
 * @return Natknow\EditorBundle\Entity\Article 
 */
public function getArticle()
{
    return $this->article;
}

/**
 * Set category
 *
 * @param Natknow\EditorBundle\Entity\Category $category
 */
public function setCategory(\Natknow\EditorBundle\Entity\Category $category)
{
    $this->category = $category;
}

/**
 * Get category
 *
 * @return Natknow\EditorBundle\Entity\Category 
 */
public function getCategory()
{
    return $this->category;
}
[...]

Category.php - 类别表实体

[...]
/**
 * Add articles
 *
 * @param Natknow\EditorBundle\Entity\ArticleCategory $articles
 */
public function addArticleCategory(\Natknow\EditorBundle\Entity\ArticleCategory $articles)
{
    $this->articles[] = $articles;
}

/**
 * Get articles
 *
 * @return Doctrine\Common\Collections\Collection 
 */
public function getArticles()
{
    return $this->articles;
}
[...]

控制器:

[...]
$art = new Article();

        $em = $this->getDoctrine()
            ->getEntityManager();
        $repo = $this->getDoctrine()
            ->getRepository('NatknowEditorBundle:Article');

        $success = "<h3>Use this form to add an article:</h3>";

        $form = $this->createForm(new ArticleType(), $art);

        if ($request->getMethod() == 'POST') {
            $form->bindRequest($request);

            if ($form->isValid()) {
                $em = $this->getDoctrine()->getEntityManager();
                $em->persist($art);
                $em->flush();

                $success = "You have successfully added the article '"
                . $art->getName() . "'!";

                $articles = $repo->findAllPastDay();
            }else {
                $success = "There was an error validating the form data!";
            }

            return $this->render('NatknowEditorBundle:Default:insertArticle.html.php',
                array('form' => $form->createView(), 'status' => $success, 'arts' => $articles,)
            );
        }
[...]

单挑:我使用命令行实用程序生成ArticleCategory.php实体,没有错误。但是,我并不是100%肯定它是为了做我想做的事。

3 个答案:

答案 0 :(得分:1)

您需要在文章实体中添加setCatagories()才能超过发布的错误。

可能遇到其他问题。让许多表单按需运行可能会很棘手。

熟悉:

http://symfony.com/doc/current/cookbook/form/form_collections.html

答案 1 :(得分:1)

你应该将addArticleCategory()命名为addCategory(),然后Symfony(主人)才能认出它。

答案 2 :(得分:0)

不要忘记使用:

Doctrine\Common\Collections\ArrayCollection;

public function __construct()
{
    $this->categories = new ArrayCollection();
}

(Article.php)

不确定这是不是问题,但我碰到了类似的事情。