-1

I have a scenario where employees and organizations tables are mapped in the separate table called org_employees.

I have no issue in adding the rows for the employees table and organization tables individually, but my question is how can i add the records for the org_employees table. Do i need to create a separate entity for org_employees table mean, if yes how the ORM relation happen. can you anyone guide me on this.

Employee Entity:

<?php
namespace Employee\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Library\Entity\BaseEntity;

/**
 * @ORM\Entity
 * @ORM\Table(name="employees")
 */ 
class Employee extends BaseEntity{

    /**
     * @ORM\Column(name="employee_code", type="string")
     * @var string
     */
    protected $empCode;       

    public function getEmpCode() {
        return $this->empCode;
    }

    public function setEmpCode($empCode) {
        $this->empCode = $empCode;
        return $this;
    }

    public function __toString() {
        return __CLASS__ . ": [id: {$this->id}, name: {$this->name}]";
    }
}

Organization Entity:

<?php

namespace Organization\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Library\Entity\BaseEntity;   

/**
 * @ORM\Entity
 * @ORM\Table(name="organizations")
 */
class Organization extends BaseEntity{

    /**
     * @ORM\Column(name="name", type="string")
     * @var string
     */
    protected $name; 

    public function getName() {
        return $this->name;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function __toString() {
        return __CLASS__ . ": [id: {$this->id}, name: {$this->name}]";
    }
}
Wilt
  • 41,477
  • 12
  • 152
  • 203
user3929758
  • 233
  • 2
  • 3
  • 15
  • you are probably looking for many to many relationships: http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#many-to-many-unidirectional – marcosh Feb 03 '16 at 16:30
  • nearly yes, but how can i persist the data – user3929758 Feb 03 '16 at 16:33
  • try to have a look here: http://stackoverflow.com/questions/17130572/doctrine-many-to-many-insert or here: http://stackoverflow.com/questions/7044864/symfony2-doctrine-manytomany-relation-is-not-saved-to-database – marcosh Feb 03 '16 at 16:37
  • Depends on if OrgEmployees have any properties or not. If no properties then using a Doctrine ManyToMany relation will do all the work for you. Persistence is handled automagically. If you need additional properties then you will need an OrgEmployeer entity with OneToMany relations to Org and Employee and persistence is handled like any other entity. – Cerad Feb 03 '16 at 17:27

1 Answers1

1

You can do two things.

  1. Add a ManyToMany relationship between the Employee and Organization.

  2. Add an additional entity EmployeeOrganization(Link) in between. It would mean:

    Employee - ManyToOne - EmployeeOrganization(Link) - OneToMany - Organization

This solution is also mentioned here in the doctrine documentation:

Why are many-to-many associations less common? Because frequently you want to associate additional attributes with an association, in which case you introduce an association class. Consequently, the direct many-to-many association disappears and is replaced by one-to-many/many-to-one associations between the 3 participating classes.

The first solution is the simplest when it comes down to your entity definitions but it only allows you to do the linking and nothing more. The advantage of the second solution is that you can add additional fields to your join table (in other words properties or attributes to your entity) . For example a field created_at that tells you when the employee was added to the organization.

If additional properties like that are not needed you can do with ManyToMany.

On how to define these relationships I would like to refer to the documentation or other stackoverflow questions, there is enough information out there.

Community
  • 1
  • 1
Wilt
  • 41,477
  • 12
  • 152
  • 203
  • The second solution is the necessary solution for me, because i have the scenario of extra columns in my link table. – user3929758 Feb 04 '16 at 10:02
  • Can you please guide me for first solution? some link of how to do it? I have to add.remove multiple rows in mapped tables – Volatil3 Jan 03 '17 at 04:14
  • @Volatil3 Chack the docs [here for unidirectional](http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#many-to-many-unidirectional) or [here for bidirectional](http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#many-to-many-bidirectional) `ManyToMany` relationships in doctrine. If you don't manage using the docs you can leave another comment. – Wilt Jan 05 '17 at 17:23