Php Symfony2从具有多个关系的倒排实体获取对象
我正面临着我的关系问题。 事情是这样的: 我有两个实体:物品和类别 文章是主人,范畴是奴隶 我想从文章中获取类别,另一种方式,从类别中获取文章 我建立了很多这样的关系:Php Symfony2从具有多个关系的倒排实体获取对象,php,symfony,orm,doctrine-orm,entity-relationship,Php,Symfony,Orm,Doctrine Orm,Entity Relationship,我正面临着我的关系问题。 事情是这样的: 我有两个实体:物品和类别 文章是主人,范畴是奴隶 我想从文章中获取类别,另一种方式,从类别中获取文章 我建立了很多这样的关系: class Article { /** * @ORM\ManyToMany(targetEntity="Alpha\BlogBundle\Entity\Category", cascade={"persist"}, inversedBy="Article") * @ORM\JoinTable(name
class Article
{
/**
* @ORM\ManyToMany(targetEntity="Alpha\BlogBundle\Entity\Category", cascade={"persist"}, inversedBy="Article")
* @ORM\JoinTable(name="article_category")
*/
private $categories;
及
以及类别内实体:
class Category
{
/**
* @ORM\ManyToMany(targetEntity="Alpha\BlogBundle\Entity\Article", cascade={"persist"}, mappedBy="Category")
*/
private $articles;
及
在我的文章实体中,我还添加了以下内容:
public function addCategory(\Alpha\BlogBundle\Entity\Category $categories)
{
$this->categories[] = $categories;
$categories->addArticle($this);
return $this;
}
(第四行,$categories->addArticle($this);
)
在我的控制器中:
public function ajouterAction($data = null, $id = null) {
// On récupère l'EM pour enregistrer en BDD
$em = $this->getDoctrine()->getManager();
// On définit une nouvel objet Article avec de nouveaux attributs
$article = new Article;
$article->setTitle('1er article !');
$article->setContent('Cupcake ipsum dolor sit amet ice cream tiramisu unerdwear.com. Caramels halvah lollipop apple pie soufflé. Tart lollipop soufflé candy tootsie roll sweet donut. Lemon drops danish I love icing I love. Candy canes cheesecake I love. I love tiramisu applicake. I love gingerbread soufflé sweet roll muffin. Cupcake liquorice gummi bears muffin chocolate jelly-o.');
$article->setAuthor('Toto');
// On définit une nouvel objet Category avec de nouveaux attributs
$category = new Category;
$category->setName('Poney');
$article->addCategory($category);
$em->persist($category);
$em->persist($article);
$em->flush();
return $this->render('AlphaBlogBundle:Blog:ajouter.html.twig');
}
最后,从分类中获取我的文章:
public function categoryAction($cat = null) {
$em = $this->getDoctrine()->getManager();
// Si cat est vide, on renvoit la liste complète des catégories
if (!isset($cat) || empty($cat) || $cat == null) {
$categories = $em->getRepository('AlphaBlogBundle:Category')->findAll();
return $this->render('AlphaBlogBundle:Blog:categories.html.twig', array(
'categories' => $categories
));
}
// Sinon on renvoit la liste des articles de la catégorie
else {
$category = $em->getRepository('AlphaBlogBundle:Category')->findOneBy(array('name' => $cat));
$articles = $category->getArticles();
return $this->render('AlphaBlogBundle:Blog:category.html.twig', array(
'articles' => $articles,
'category' => $category
//'name' => $name
));
}
}
在我看来,我可以看到我的类别名称,但文章没有显示,我有以下错误消息:
ContextErrorException:Notice:Undefined index:Category in/home/frank/www/alpha/vendor/doctrine/orm/lib/doctrine/orm/Persisters/basicitypersister.php第1036行
如果有人能帮忙的话,我在这里有点迷路。如下:
mappedBy:此选项指定targetEntity上的属性名称
这就是这种关系的拥有方。它是
关系的反面
inversedBy:inversedBy属性指定实体中的字段
这是关系的反面
因此,尝试将文章类中的inversedBy=“Article”
更改为inversedBy=“articles”
,并将类别类中的mappedBy=“Category”
更改为mappedBy=“categories”
另请参见多对多双向示例
希望这能有所帮助。好吧,我听从你的建议,一篇一篇地修改文章,一个类别一个类别地修改。我还更改了主机端的映射,文章,如下所示:
/**
* @ORM\ManyToMany(
* targetEntity="Alpha\BlogBundle\Entity\Category",
* cascade={"persist"},
* inversedBy="articles"
* )
* @ORM\JoinTable(name="article_category",
* joinColumns={@ORM\JoinColumn(name="article_id", referencedColumnName="id")},
* inverseJoinColumns={
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
* }
* )
*/
它就像一个符咒。我不太明白,但我要研究一下:)
public function categoryAction($cat = null) {
$em = $this->getDoctrine()->getManager();
// Si cat est vide, on renvoit la liste complète des catégories
if (!isset($cat) || empty($cat) || $cat == null) {
$categories = $em->getRepository('AlphaBlogBundle:Category')->findAll();
return $this->render('AlphaBlogBundle:Blog:categories.html.twig', array(
'categories' => $categories
));
}
// Sinon on renvoit la liste des articles de la catégorie
else {
$category = $em->getRepository('AlphaBlogBundle:Category')->findOneBy(array('name' => $cat));
$articles = $category->getArticles();
return $this->render('AlphaBlogBundle:Blog:category.html.twig', array(
'articles' => $articles,
'category' => $category
//'name' => $name
));
}
}
/**
* @ORM\ManyToMany(
* targetEntity="Alpha\BlogBundle\Entity\Category",
* cascade={"persist"},
* inversedBy="articles"
* )
* @ORM\JoinTable(name="article_category",
* joinColumns={@ORM\JoinColumn(name="article_id", referencedColumnName="id")},
* inverseJoinColumns={
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
* }
* )
*/