Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7843161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:29:12+00:00 2026-06-02T16:29:12+00:00

How can i create a relationship between entities with Symfony 2 and Doctrine? I’m

  • 0

How can i create a relationship between entities with Symfony 2 and Doctrine? I’m only able to create standalone entities. Maybe someone can help me figure this out using the entity generator? I want to:

  • Create two entities: Post and Category. A Post is part of a Category.
  • Create a Tag entity: A Post can have many Tags.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:29:12+00:00Added an answer on June 2, 2026 at 4:29 pm

    A practical example is covered in Symfony2 docs here:

    http://symfony.com/doc/current/book/doctrine.html#entity-relationships-associations

    To elaborate, taking the first example, you need to create a OneToMany relationship between your Category object and your Post object:

    Category.php:

    <?php
    
    namespace Your\CustomBundle\Entity;
    
    use Doctrine\ORM\Mapping as ORM;
    use Doctrine\Common\Collections\ArrayCollection;
    
    /**
     * @ORM\Table(name="category")
     * @ORM\Entity()
     */
    class Category
    {
        /**
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
    
        /**
         * @ORM\OneToMany(targetEntity="Post", mappedBy="category")
         */
        public $posts;
    
        /**
         * Constructor
         */
        public function __construct()
        {
            $this->posts = new ArrayCollection();
        }
    
        /**
         * @return integer 
         */
        public function getId()
        {
            return $this->id;
        }
    }
    

    Post.php

    <?php
    
    namespace Your\CustomBundle\Entity;
    
    use Doctrine\ORM\Mapping as ORM;
    
    /**
     * @ORM\Table(name="post")
     * @ORM\Entity()
     */
    class Post
    {
        /**
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
    
        /**
         * @ORM\ManyToOne(targetEntity="Category", inversedBy="posts")
         */
        public $category;
    
        /**
         * @return integer 
         */
        public function getId()
        {
            return $this->id;
        }
    }
    

    This should get you started. I’ve just written this so there might be errors :s

    I’m making properties $posts and $category public here for brevity; however you’d probably be advised to make these private and add setters/getters to your classes.

    Also note that $posts is an array-like Doctrine ArrayObject class especially for arrgregating entities, with methods like $category->posts->add($post) etc.

    For more detail look into association mapping in the Doctrine documentation. You’ll probably need to set up a ManyToMany relationship between Posts and Tags.

    Hope this helps 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone help? I have a created a relationship between my Reservation(prim key) and
Experts, In XAML I would like to create a many-to-many relationship between entities. Basically
I have two tables that I'm trying to create a relationship between so I
I have a one to many entity relationship between two entities: EntityP (Parent) <-->>
I have entities Post and Tag , where there is a many-to-many relationship between
I want to create a MySQL table to hold relationship data between users. The
I have a 1 to many relationship between 2 entities in my model. I
I am trying to create a many-many relationship between the max(id) in table1 with
I have a table which defines a child-parent relationship between nodes: CREATE TABLE node
I have a many-to-many relationship between Prequalification and Company entities ( Partnership ). The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.