简体   繁体   中英

Symfony2 Get entities of an entity in another entity

This question is about Symfony2 table relationships using ORM. I have three tables/entities that are related to each other. The relationship is very similar to Wordpress Posts, Categories and Categories relationship tables.

Table 1 contains posts. Table 2 contains categories Table 3 contains relationships between the categories and posts.

I want to be able to have the categories property in the posts table and a posts property in the categories table. So that when I call.

Categories->posts : I should get posts in that category. Posts->categories : I should get the categories the post belongs to.

I want to have unique categories per table and I want all posts to point to a category without having to create a new entry for the category that already exists which is what ManyToOne or OneToMany is offering this is why the third table I think is necessary.

For example here is the relationships

class Category_relationship
{

    /**
     * @var integer
     *
     * @ORM\Column(name="object_id", type="bigint")
     *
     * @ORM\ManyToOne(targetEntity="Worksheet",         inversedBy="category_relationships")
     * @ORM\JoinColumn(name="worksheet_id", referencedColumnName="id", nullable=FALSE)
     */
    private $objectId;

    /**
     * @var integer
     *
     * @ORM\Column(name="category_id", type="bigint")
     *
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="categories")
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id", nullable=FALSE)
     */
    private $categoryId;

}

Here is the Category class:

class Category
{
    /**
     * @ORM\OneToMany(targetEntity="Category_relationship", mappedBy="categoryId", cascade={"persist", "remove"}, orphanRemoval=TRUE)
     */
    protected $posts;
}

Here is the Category class:

class Posts
{    /**
     * @ORM\OneToMany(targetEntity="Category_relationship", mappedBy="objectId", cascade={"persist", "remove"}, orphanRemoval=TRUE)
     */
    protected $categories;
}

I want to create a system where I can assign posts to a category but the category table can only contain 1 entry about the category. I also want to be able to use expressions link;

Post->categories Category->posts

or

Post->AddCategory() Category->AddPost()

Thanks for your help.

It seems that you want a simple many-to-many relationship.

Every post can have multiple categories, and every category have list of related posts. Many to many handles pivot table by itself.

So, in Post entity you have to declare relationship that way:

/**
 * @ORM\ManyToMany(targetEntity="Category", inversedBy="posts")
 * @ORM\JoinTable(name="PostsCategories",
 *      joinColumns={@ORM\JoinColumn(name="post_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="id")}
 *      )
 **/
protected $categories;

Remember about using Doctrine\\ORM\\Mapping with ORM alias (you don't have to import all subclasses separately):

use Doctrine\ORM\Mapping as ORM;

After that, you need to create a new ArrayCollection in class constructor:

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

And add proper methods, like addCategory:

public function addCategory(Category $category)
{
    $this->categories[] = $category;

    return $this;
}

You can also add them automatically with:

php app/console doctrine:generate:entities BundleName:EntityName

Same thing in Category entity, but with a little different definiton:

 /**
 * @ORM\ManyToMany(targetEntity="Post", mappedBy="categories")
 **/
protected $posts;

You can find all of these information in Doctrine docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM