Doctrine 2链接表关系 - 完整性约束违规

时间:2016-02-04 19:19:22

标签: symfony doctrine-orm zend-framework2

我有一个场景,我将链接以下表格:

组织 雇员 org_employees

所以我用以下方式链接这些行为。

员工 - OneToMany - OrgEmployee(链接表) - ManyToOne

但我收到以下错误:

An exception occurred while executing 'INSERT INTO org_branch_employees (is_active, created_at, created_by, modified_at, modified_by, employee_id, org_branch_id) VALUES (?, ?, ?, ?, ?, ?, ?)' with params [\"1\", \"2016-02-04 20:03:22\", 1, \"2016-02-04 20:03:22\", 1, null, null]:\n\nSQLSTATE[23000]: Integrity constraint violation: 1048 Column 'employee_id' cannot be null"

我很沮丧,我错了,任何人都可以帮助我。

员工实体:

<?php

namespace Employee\Entity;

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

use Users\Entity\User;
use Organization\Entity\Organization;
//use Organization\Entity\OrgEmployee;

/**
 * Description of Employee
 *
 * @author Macwin
 */

/**
 * @ORM\Entity
 * @ORM\Table(name="employees")
 */

class Employee extends BaseEntity{

    /**
     * @ORM\OneToOne(
     *     targetEntity="Users\Entity\User"
     * )
     * @ORM\JoinColumn(
     *     name="user_id",
     *     referencedColumnName="id",
     *     nullable=false
     * )
     */
    private $user;

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

    /**
     * @ORM\OneToOne(
     *     targetEntity="Organization\Entity\Organization"
     * )
     * @ORM\JoinColumn(
     *     name="org_id",
     *     referencedColumnName="id",
     *     nullable=false
     * )
     */
    private $organization;

    /**
     * @ORM\OneToMany(targetEntity="Employee\Entity\OrgEmployee", mappedBy="employee")
     */
    protected $orgEmployee;

    public function __construct() {

        $this->organizations = new \Doctrine\Common\Collections\ArrayCollection();

    }

    public function getOrganizations()
    {
        return $this->organizations;
    }

    public function addOrganization(Organization $organization = null)
    {
        $this->organizations->add($organization);
    }

    public function setUser(User $user = null)
    {
        $this->user = $user;

        return $this;
    }

    public function getUser()
    {
        return $this->user;
    }

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

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

    public function setOrganization(Organization $organization = null)
    {
        $this->organization = $organization;

        return $this;
    }

    public function getOrganization()
    {
        return $this->organization;
    }

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

这是我的组织实体:

 <?php

    namespace Organization\Entity;

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

    use Organization\Entity\OrganizationType;
    use Organization\Entity\OrgEmployee;


    /**
     * Description of Organization
     *
     * @author Macwin
     */

    /**
     * @ORM\Entity
     * @ORM\Table(name="organizations")
     */

    class Organization extends BaseEntity{



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

        /**
         * @ORM\OneToOne(
         *     targetEntity="Organization\Entity\OrganizationType"
         * )
         * @ORM\JoinColumn(
         *     name="org_type_id",
         *     referencedColumnName="id",
         *     nullable=false
         * )
         */
        private $orgType;

        /**
         * 
         * @ORM\OneToOne(targetEntity="Organization\Entity\Organization")
         * @ORM\JoinColumn(name="parent_org_id", referencedColumnName="id")
         */
        protected $parent;

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

        /**
         * @var string
         *
         * @ORM\Column(name="address_line1", type="string", length=255, nullable=true)
         */
        private $addressLine1;

        /**
         * @var string
         *
         * @ORM\Column(name="address_line2", type="string", length=255, nullable=true)
         */
        private $addressLine2;

        /**
         * @var string
         *
         * @ORM\Column(name="zipcode", type="string", length=255, nullable=true)
         */
        private $zipcode;

        /**
         * @ORM\OneToOne(
         *     targetEntity="Library\Entity\Country"
         * )
         * @ORM\JoinColumn(
         *     name="country",
         *     referencedColumnName="id",
         *     nullable=false
         * )
         */
        private $country;

        /**
         * @ORM\OneToOne(
         *     targetEntity="Library\Entity\State"
         * )
         * @ORM\JoinColumn(
         *     name="state",
         *     referencedColumnName="id",
         *     nullable=false
         * )
         */
        private $state;


        /**
         * @ORM\OneToOne(
         *     targetEntity="Library\Entity\City"
         * )
         * @ORM\JoinColumn(
         *     name="city",
         *     referencedColumnName="id",
         *     nullable=false
         * )
         */
        private $city;


        /**
         * @ORM\OneToOne(
         *     targetEntity="Library\Entity\Area"
         * )
         * @ORM\JoinColumn(
         *     name="area",
         *     referencedColumnName="id",
         *     nullable=false
         * )
         */
        private $area;


        /**
         * @ORM\OneToMany(targetEntity="Employee\Entity\OrgEmployee", mappedBy="organization")
         */
        protected $orgEmployee;

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

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

        public function setOrgType(OrganizationType $orgType = null)
        {
            $this->orgType = $orgType;

            return $this;
        }

        public function getOrgType()
        {
            return $this->orgType;
        }

        public function getParent() {
            return $this->parent;
        }

        public function setParent($parent) {
            $this->parent = $parent;
        }

        public function getDescription() {
            return $this->description;
        }

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

这是OrgEmployee实体:

<?php

namespace Employee\Entity;

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

/**
 * Description of Org Employees
 *
 * @author Macwin
 */

/**
 * @ORM\Entity
 * @ORM\Table(name="org_branch_employees")
 */

class OrgEmployee extends BaseEntity{

    /**
     * @ORM\ManyToOne(targetEntity="Employee\Entity\Employee", inversedBy="orgEmployee")
     * @ORM\JoinColumn(name="employee_id",referencedColumnName="id",nullable=false)
     */
    protected $employee;

    /**
     * @ORM\ManyToOne(targetEntity="Organization\Entity\Organization", inversedBy="orgEmployee")
     * @ORM\JoinColumn(name="org_branch_id", referencedColumnName="id", nullable=false)
     */
    protected $organization;

    public function setEmployees(Employee $employee = null)
    {
        $this->employees = $employee;

        return $this;
    }

    public function getEmployees()
    {
        return $this->employees;
    } 

    public function setOrganizations(Organization $organization = null)
    {
        $this->organizations = $organization;

        return $this;
    }

    public function getOrganizations()
    {
        return $this->organizations;
    }    


}

2 个答案:

答案 0 :(得分:0)

OrgEmployee实体存在问题,应该是setEmployee()和setOrganization(),我有$ this-&gt;员工而不是$ this-&gt;员工

    class OrgEmployee extends BaseEntity{

        /**
         * @ORM\ManyToOne(targetEntity="Employee\Entity\Employee", inversedBy="orgEmployee")
         * @ORM\JoinColumn(name="employee_id",referencedColumnName="id",nullable=false)
         */
        protected $employee;

        /**
         * @ORM\ManyToOne(targetEntity="Organization\Entity\Organization", inversedBy="orgEmployee")
         * @ORM\JoinColumn(name="org_branch_id", referencedColumnName="id", nullable=false)
         */
        protected $organization;

        public function setEmployee(Employee $employee = null)
        {
            $this->employee = $employee;

        return $this;
    }

    public function getEmployee()
    {
        return $this->employee;
    } 

    public function setOrganization(Organization $organization = null)
    {
        $this->organization = $organization;

        return $this;
    }

    public function getOrganization()
    {
        return $this->organization;
    }    


}

答案 1 :(得分:0)

据我所知,组织有很多员工,员工可以属于很多组织。您不需要创建新实体。它应该是这样的:

员工人数

/**
 * @var Organization | ArrayCollection
 *
 * @ORM\ManyToMany(targetEntity="Organization\Entity\Organization", mappedBy="employees")
 */
protected $organizations;

/**
 * @return ArrayCollection|Organization
 */
public function getOrganizations()
{
    return $this->organizations;
}

/**
 * @param Organization $organization
 * @return $this
 */
public function addOrganization(Organization $organization)
{
    if (!$this->organizations->contains($organization)) {
        $this->organizations->add($organization);
    }

    return $this;
}

/**
 * @param Organization $organization
 * @return $this
 */
public function removeOrganization(Organization $organization)
{
    if ($this->organizations->contains($organization)) {
        $this->categories->removeElement($category);
    }

    return $this;
}

组织类

/**
 * @var Employee | ArrayCollection
 *
 * @ORM\ManyToMany(targetEntity="Employee\Entity\Employee", inversedBy="organizations")
 * @ORM\JoinTable(name="org_employees",
 *      joinColumns={@ORM\JoinColumn(name="organization_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="employee_id", referencedColumnName="id")}
 *      )
 */
protected $employees;

getter, add, remove methods here..

注意我的代码中的错误(我快速写了这篇文章) - 它应该只显示概念。