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

问题描述 投票:1回答:1

我试图使用比id更多的属性建立manyToMany关系,所以我需要两个OneToMany关系和两个具有三个表/实体的ManytoOne关系。

我有Product实体,Client实体和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;
}

这就是这样的(有吸气剂和制定者以及更多属性)。但是,当我进入产品问题时,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。

如果我去客户端,我也会得到同样的结果。怎么了?

symfony doctrine many-to-many relation symfony3.x
1个回答
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;
}
© www.soinside.com 2019 - 2024. All rights reserved.