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

  • SEARCH
  • Home
  • 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 8652119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:14:04+00:00 2026-06-12T14:14:04+00:00

Doctrine has got some nice documentation, but at some point I have the feeling

  • 0

Doctrine has got some nice documentation, but at some point I have the feeling for someone who wants to get in to doctrine it is sort of a small battle to get used to the mapping stuff. I am one of those guys who belongs to this section. I have gone through most of the mapping stuff documentation for example like this and other links in doctrine official site, but the documentation for me looks like bits and pieces to follow. I am saying this for my case.

Is there somewhere an example which shows how can I join two tables with a third join table, I wanted to know the basic mapping for this schema.

Let me say I have two tables: Fruits and Country.

The relationship is that one country produces many varieties of fruits, so as to say that is a onetomany and manytoone relationship. Apart from that I wanted to do association using a third table say countryFruits.

Fruits Table
-- fruitsId (PK, AI)
-- fruitName

Country Table
-- countryId (PK, AI)
-- countryName

countryFruits Table
-- fruitsId (PK, FK)
-- countryId (PK, FK)

That is how the tables in MySQL look like and it has already been designed. Now I can fill the fruits table with doctrine and when it comes to filling the country table, I get a messed up mapping problem.

/**
 * @ORM\Entity
 * @ORM\Table(name="fruits")
 * @property string $fruitName
 * @property int $fruitId
 */
class Fruits
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer", name="fruitId", unique=true);
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $fruitId;

    /**
     * @ORM\Column(type="string")
     */
    protected $fruitName;

    /**
     * @ORM\OneToMany(targetEntity="Country", mappedBy="fruits", cascade={"persist"})
     */
    protected $country;

    public function __get($property)
    {
        return $this->$property;
    }

    public function __set($property, $value)
    {
        $this->$property = $value;
    }

}

/**
 * @ORM\Entity
 * @ORM\Table(name="country")
 * @property string $countryName
 * @property int $countryId
 */
class Country
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer", name="countryId", unique=true);
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $countryId;

    /**
     * @ORM\Column(type="string")
     */
    protected $countryName;

    /**
     * @ORM\OneToMany(targetEntity="Fruits", mappedBy="country", cascade={"persist"})
     */
    protected $countries;

    public function __get($property)
    {
        return $this->$property;
    }

    public function __set($property, $value)
    {
        $this->$property = $value;
    }

}

/**     
 * @ORM\Entity
 * @ORM\Table(name="countryFruits ")
 * @property int $fruitId
 * @property int $countryId
 */
class countryFruits
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer", name="fruitId", nullable=false)
     * @ORM\GeneratedValue(strategy="NONE")
     */
    protected $fruitId;

    /**
     * @ORM\Id
     * @ORM\Column(type="integer", name="countryId", nullable=false)
     * @ORM\GeneratedValue(strategy="NONE")
     */
    protected $countryId;

    /**
     * @ORM\ManyToOne(targetEntity="Country", inversedBy="fruits", cascade={"persist"})
     * @ORM\JoinColumn(name="countryId", referencedColumnName="countryId")
     */
    protected $country;

    /**
     * @ORM\ManyToOne(targetEntity="Fruits", inversedBy="country", cascade={"persist"})
     * @ORM\JoinColumn(name="fruitId", referencedColumnName="fruitId")
     */
    protected $fruits;

    /**
     * Set fruits
     *
     * @param Fruits $fruits
     */
    public function setFruits($fruits)
    {
        $this->fruits = $fruits;
    }

    /**
     * ´Get fruits
     *
     * @param Fruits $fruits
     */
    public function getFruits()
    {
        return $this->fruits;
    }

    /**
     * Set country
     *
     * @param Country $country
     */
    public function setCountry($country)
    {
        $this->country = $country;
    }

    /**
     * Get country
     *
     * @param Country $country
     */
    public function getCountry($country)
    {
        $this->country = $country;
    }
}

Can somebody cross check this and let me know if my mappings are done in the proper way. In case, is it possible to get a small piece of code how to persist the entities into my database.

  • 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-12T14:14:05+00:00Added an answer on June 12, 2026 at 2:14 pm

    you don’t need the countryFruits class. what you’re searching for is the ManyToMany relation! also you don’t want to name entities in plural, as an entity always represends a single object/row in the table.

    Fruit entity

    /**
     * @ORM\Entity
     * @ORM\Table(name="fruits")
     * @property string $fruitName
     * @property int $fruitId
     */
    class Fruit
    {
        /**
          * @ORM\ManyToMany(targetEntity="Country")
          * @ORM\JoinTable(name="country_fruits",
          *         joinColumns={@ORM\JoinColumn(name="country_id", referencedColumnName="id")},
          *         inverseJoinColumns={@ORM\JoinColumn(name="fruit_id", referencedColumnName="id")}
          * ) 
          * @var Country[]
          */
          protected $countries;
    }
    

    Country entity

    /**
     * @ORM\Entity
     * @ORM\Table(name="country")
     * @property string $countryName
     * @property int $countryId
     */
    class Country
    {
        /**
          * @ORM\ManyToMany(targetEntity="Fruit")
          * @ORM\JoinTable(name="country_fruits",
          *         joinColumns={@ORM\JoinColumn(name="fruit_id", referencedColumnName="id")},
          *         inverseJoinColumns={@ORM\JoinColumn(name="country_id", referencedColumnName="id")}
          * ) 
          * @var Fruit[]
          */
          protected $fruits;
     }
    

    note that you don’t need the countryFruits class, but the table country_fruits is required!

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

Sidebar

Related Questions

Using Doctrine 2 I want to get some users that are contacts of another
I followed doctrine documnetation to get started. Here is the documentation . My code
I have the need of get the array representation of a Doctrine 2 Entity.
I have a doctrine model that has a method getSomethingId() and I'm making something
I have three Doctrine entities: Device, which has a OneToOne relationship with Device\Status, which
I have a data model in Doctrine/symfony. I have a 'Course' which has many
I get table - $data = Doctrine::getTable('product_catalog')->findAll(); this table (product_catalog) i link table. has
I have two entities in Doctrine 2.1: Category and Site each category has many
I am working on an application (Symfony 1.4 / Doctrine) that has some pages
I have an object that has an instance of Doctrine\ODM\MongoDB\Query\Builder . When I serialize

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.