Symfony3-关联指的是ManyToMany和字段表不存在的拥有方字段

时间:2019-02-19 15:10:03

标签: symfony doctrine many-to-many relation symfony3.x

我正在尝试使用比ID多的属性来创建manyToMany关系,因此我需要两个OneToMany关系和两个具有三个表/实体的ManytoOne关系。

我有产品实体,客户实体和ProductClient实体:

class Client 
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id_client", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $idClient;

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

    /**
     * @var \ProductClient
     *
     * @ORM\OneToMany(targetEntity="ProductClient", mappedBy="client")
     */
    private $products_clients;

}

class Product
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id_product", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $idProduct;

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

    /**
     * @var \ProductClient
     *
     * @ORM\OneToMany(targetEntity="ProductClient", mappedBy="product")
     */
    private $products_clients;
}

class ProductClient
{

    /**
     * @ORM\Column(name="product_id", type="integer")
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="Product", inversedBy="products_clients")
     * @ORM\JoinColumn(name="product_id", referencedColumnName="id_product", nullable=false)
     */
    protected $product;

    /**
     * @ORM\Column(name="client_id", type="integer")
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="Client", inversedBy="products_clients")
     * @ORM\JoinColumn(name="client_id", referencedColumnName="id_client", nullable=false)
     */
    protected $client;

    /**
     * @var bool
     *
     * @ORM\Column(name="status", type="boolean")
     */
    private $status;
}

就是这样(带有其getter和setter以及更多属性)。但是当我进入产品分类时,symfony会启动两个“无效实体错误”:

  

AppBundle \ Entity \ Product-关联AppBundle \ Entity \ Product#products_clients是指拥有方字段AppBundle \ Entity \ ProductClient#product,该字段未定义为关联,而是字段。

     

AppBundle \ Entity \ Product-关联AppBundle \ Entity \ Product#products_clients指不存在的拥有方字段AppBundle \ Entity \ ProductClient#product。

同样的结果,如果我去客户端程序。怎么了?

1 个答案:

答案 0 :(得分:0)

您会在错误消息AppBundle\Entity\ProductClient#product is not defined as association, but as field中看到。

只需删除此@ORM\Column(name="product_id", type="integer")和此@ORM\Column(name="client_id", type="integer")

class ProductClient
{
    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="Product", inversedBy="products_clients")
     * @ORM\JoinColumn(name="product_id", referencedColumnName="id_product", nullable=false)
     */
    protected $product;

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="Client", inversedBy="products_clients")
     * @ORM\JoinColumn(name="client_id", referencedColumnName="id_client", nullable=false)
     */
    protected $client;

    /**
     * @var bool
     *
     * @ORM\Column(name="status", type="boolean")
     */
    private $status;
}