我有一个提交文章的表格。该表单有一个类别多选框,它使用我的 Category.php 实体从数据库中提取类别。表单生成正常,但尝试提交时出现以下错误:
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% 确定它是否可以做我想做的事情。