在ManytoMany重复条目中捕获错误

时间:2018-11-30 09:02:26

标签: symfony doctrine

在Symfony3.4中,我试图合并两个实体,如果已经给出,则出现“重复条目”错误。这是通过学说中的Many2Many关系完成的。

用户:公司<--- Many2Many --->公司:用户

如何在响应控制器的操作中捕获错误?

用户实体:

    /**
     * @ORM\Table(name="md_user")
     * @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
     * @Vich\Uploadable
     * @ApiResource(
     *     collectionOperations={
     *      "get"={"method"="GET"},
     *     "post"={"method"="POST"}
     *     },
     *     itemOperations={
     *      "get"={"method"="GET", "access_control_message"="Sorry, but you are not allowed to do this."},
     *      "put"={"method"="PUT"}
     *         },
     *     attributes={"access_control"="is_granted('ROLE_USER')"})
     * @ORM\AttributeOverrides({
     *      @ORM\AttributeOverride(name="usernameCanonical",
     *          column=@ORM\Column(
     *              name     = "username_canonical",
     *              length   = 191,
     *              unique   = true
     *          )
     *      ),
     *      @ORM\AttributeOverride(name="emailCanonical",
     *          column=@ORM\Column(
     *              name     = "email_canonical",
     *              length   = 191,
     *              unique   = false,
     *              nullable=true
     *          )
     *      )
     *
     * })
     */

    class User extends BaseUser
    {

        public function __construct()
        {
            parent::__construct();

            $this->created         = new \DateTime();
            $this->company = new ArrayCollection();
            $this->roles = array('ROLE_USER');

        }

        /**
         * @ORM\Id
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        protected $id;

        /**
         * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Company",inversedBy="users")
         * @ORM\JoinTable(name="user_comp_comp_user",
         *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
         *      inverseJoinColumns={@ORM\JoinColumn(name="company_id", referencedColumnName="id")}
         * )
         */
        protected $company;

    /**
     * @return mixed
     */
    public function getCompany()
    {
        return $this->company;
    }

    /**
     * @param mixed $company
     * @return User
     */
    public function setCompany($company)
    {
        $this->company = $company;
        return $this;
    }

/**
     * @param Company $company
     */
    public function addCompany(Company $company)
    {
        if ($this->company->contains($company)) {
            return;
        }
        $this->company[] =$company;
        return $this;
    }
    /**
     * @param Company $company
     */
    public function removeCompany(Company $company)
    {
        if (!$this->company->contains($company)) {
            return;
        }
        $this->company->removeElement($company);
        $company->removeUser($this);
    }

公司实体:

    class Company
    {

        public function __construct()
        {

            $this->created         = new \DateTime();
            $this->users = new ArrayCollection();
        }

        /**
         * @ORM\Id
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        protected $id;

        /**
         * @ORM\Column(name="mandant",type="integer",nullable=true)
         */
        protected $mandant;

        /**
         * @ORM\Column(type="integer",nullable=true)
         */
        protected $customer_group;

        /**
         * @var \Doctrine\Common\Collections\Collection|Company[]
         * @ORM\ManyToMany(targetEntity="User",mappedBy="company")
         */
        protected $users;

 /**
     * @return Company[]|\Doctrine\Common\Collections\Collection
     */
    public function getUsers()
    {
        return $this->users;
    }

    /**
     * @param Company[]|\Doctrine\Common\Collections\Collection $users
     * @return Company
     */
    public function setUsers($users)
    {
        $this->users = $users;
        return $this;
    }

/**
     * @param User $user
     */
    public function addUser(User $user)
    {
        if ($this->users->contains($user)) {
            return;
        }
        $this->users[] =$user;
        return $this;
    }
    /**
     * @param User $user
     */
    public function removeUser(User $user)
    {
        if (!$this->users->contains($user)) {
            return;
        }
        $this->users->removeElement($user);
        $user->removeCompany($this);
    }

简化的控制器操作:

$user = $this->getDoctrine()->getRepository(User::class)->find($id);
$company = $this->getDoctrine()->getRepository(Company::class)->find($company_id);
$user->addCompany($company);
try {
            $this->getDoctrine()->getManager()->merge($user);
            $this->getDoctrine()->getManager()->flush();
        }catch (UniqueConstraintViolationException $e) {
                return new JsonResponse(array('success' => false,'error'=> $e->getMessage()));
        }

1 个答案:

答案 0 :(得分:0)

我自己找到的。 要在刷新之前检查重复项,可以在addCompany()中使用User

public function addCompany(Company $company)
    {
        if ($this->company->contains($company)) {
            return;
        }
        $this->company[] =$company;
        return $this;
    }

这将检查是否已输入。如果没有,它将返回。

在控制器中,您可以执行以下操作:

if(!$user->addCompany($company)){
    return new JsonResponse(array('success' => false,'error'=> 'duplicate entry'));
}

我希望它能对某人有所帮助。