ManyToMany不适用于继承

时间:2018-02-04 11:25:57

标签: doctrine-orm symfony4

我正在构建一个应用程序来处理给予提供者的提供者和公司。两者都继承了一个名为“组织”的超类。

我想在它们之间添加一个单向的ManyToMany关系,但是当我要求学说实现数据库并用夹具来补充它时,我无法检索公司所拥有的给予者。

Doctrine甚至没有创建一个可能包含关系信息的giver_company表。

这是我的代码:

组织

<?php
// src/Entity/Chain/Organization.php
namespace App\Entity\Chain;

use App\Entity\User;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="type", type="string")
 * @ORM\DiscriminatorMap({"association" = "Association", "super_association" = "SuperAssociation", "company" = "Company", "giver" = "Giver"})
 */

abstract class Organization
{

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

    /**
     * @ORM\Column(type="string", length=255)
     * @Assert\NotBlank()
     */
    private $name;

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

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

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

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

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

    /**
     * @ORM\OneToOne(targetEntity="App\Entity\Photo")
     */
    private $photo;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\User", mappedBy="memberOf")
     */
    private $members ;

    /**
     * Organization constructor.
     */
    public function __construct()
    {
        $this->members = new ArrayCollection();
    }


    public function getId()
    {
        return $this->id;
    }

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

    /**
     * @param mixed $name
     */
    public function setName($name): void
    {
        $this->name = $name;
    }

    public function getAddress()
    {
        return $this->address;
    }

    /**
     * @param mixed $address
     */
    public function setAddress($address): void
    {
        $this->address = $address;
    }

    public function getCity()
    {
        return $this->city;
    }

    /**
     * @param mixed $city
     */
    public function setCity($city): void
    {
        $this->city = $city;
    }

    public function getZipcode()
    {
        return $this->zipcode;
    }

    /**
     * @param mixed $zipcode
     */
    public function setZipcode($zipcode): void
    {
        $this->zipcode = $zipcode;
    }

    public function getSIREN()
    {
        return $this->SIREN;
    }

    /**
     * @param mixed $SIREN
     */
    public function setSIREN($SIREN): void
    {
        $this->SIREN = $SIREN;
    }

    public function getPhone()
    {
        return $this->phone;
    }

    /**
     * @param mixed $phone
     */
    public function setPhone($phone): void
    {
        $this->phone = $phone;
    }

    public function getPhoto()
    {
        return $this->photo;
    }

    /**
     * @param mixed $photo
     */
    public function setPhoto($photo): void
    {
        $this->photo = $photo;
    }

    public function getMembers()
    {
        return $this->members;
    }

    public function addMember(User $member)
    {
        if (!$this->members->contains($member)) {
            $this->members->add($member);
        }
    }

    public function removeMember(User $member){
        $this->members->remove($member);
    }
}

<?php
// src/Entity/Chain/Giver.php
namespace App\Entity\Chain;

use App\Entity\Photo;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 */
class Giver extends Organization
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;


    /**
     * @ORM\Column(type="string", length=255)
     */
    private $status ;


    /**
     * Giver constructor.
     */
    public function __construct()
    {
        parent::__construct();
    }

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

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

    /**
     * @param mixed $status
     */
    public function setStatus($status): void
    {
        $this->status = $status;
    }

}

公司

<?php
    // src/Entity/Chain/Company.php
    namespace App\Entity\Chain;

    use Doctrine\Common\Collections\ArrayCollection;
    use Doctrine\ORM\Mapping as ORM;

    /**
     * @ORM\Entity()
     */
    class Company extends Organization
    {
        /**
         * @ORM\Id
         * @ORM\GeneratedValue
         * @ORM\Column(type="integer")
         */
        private $id;

        /**
         * @ORM\Column(type="string", length=255, name="company_type")
         */
        private $type ;

        /*
         * @ORM\ManyToMany(targetEntity="App\Entity\Chain\Giver")
         */
        private $givers ;

        /**
         * Company constructor.
         */
        public function __construct()
        {
            parent::__construct();
            $this->givers = new ArrayCollection();
        }


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

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

        /**
         * @param mixed $type
         */
        public function setType($type): void
        {
            $this->type = $type;
        }



        public function getGivers()
        {
            return $this->givers;
        }

        public function addGiver(Giver $opening)
        {
            if (!$this->givers->contains($opening)) {
                $this->givers->add($opening);
            }
        }

        public function removeGiver(Giver $opening){
            $this->givers->remove($opening);
        }
    }

1 个答案:

答案 0 :(得分:1)

公司中提供者 DocBlock 在开头时缺少“*”。 PHPDoc

变化:

/*
* @ORM\ManyToMany(targetEntity="App\Entity\Chain\Giver")
*/
private $givers ;

/**
* @ORM\ManyToMany(targetEntity="App\Entity\Chain\Giver")
*/
private $givers ;