Php 带额外字段的原则关系MtM

Php 带额外字段的原则关系MtM,php,symfony,doctrine-orm,doctrine,Php,Symfony,Doctrine Orm,Doctrine,我有三节课: 产品 组成部分 产品组件 我想用额外的字段(组件数量)创建多对多关系,产品是由组件构建的,所以我创建了三个类,如: 产品: /** * Product * * @ORM\Table(name="product") * @ORM\Entity(repositoryClass="Justart\ProductsBundle\Repository\ProductRepository") * * @GRID\Source(columns="id,name,code,catego

我有三节课:

  • 产品
  • 组成部分
  • 产品组件
  • 我想用额外的字段(组件数量)创建多对多关系,产品是由组件构建的,所以我创建了三个类,如:

    产品:

    /**
     * Product
     *
     * @ORM\Table(name="product")
     * @ORM\Entity(repositoryClass="Justart\ProductsBundle\Repository\ProductRepository")
     *
     * @GRID\Source(columns="id,name,code,category.name,active")
     */
    class Product
    {
        /**
         * @var int
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         *
         */
        private $id;
    
        /**
         * @var string
         *
         * @ORM\Column(name="name", type="string", length=255)
         *
         */
        private $name;
    
        /**
         * @var array
         * @ORM\OneToMany(targetEntity="ProductComponent", mappedBy="product")
         *
         */
        private $components;
    }
    
    组成部分:

    /**
     * Component
     *
     * @ORM\Table(name="component")
     * @ORM\Entity(repositoryClass="Justart\ProductsBundle\Repository\ComponentRepository")
     *
     */
    
    class Component
    {
        /**
         * @var int
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         *
         * @GRID\Column(title="ID", type="number")
         */
        private $id;
    
        /**
         * @var string
         *
         * @Assert\NotBlank()
         * @ORM\Column(name="name", type="string", length=255)
         *
         */
        private $name;
    
        /**
         * @ORM\OneToMany(targetEntity="ProductComponent", mappedBy="component")
         */
        private $componentProducts;
    }
    
    产品组件:

    /**
     * ProductComponent
     *
     * @ORM\Table(name="product_component")
     * @ORM\Entity(repositoryClass="Justart\ProductsBundle\Repository\ProductComponentRepository")
     */
    class ProductComponent
    {
        /**
         * @var int
         *
         * @ORM\Id
         * @ORM\ManyToOne(targetEntity="Product", inversedBy="product_component")
         * @ORM\JoinColumn(name="product_id", referencedColumnName="id", nullable=false)
         */
        private $product;
    
        /**
         * @var int
         *
         * @ORM\Id
         * @ORM\ManyToOne(targetEntity="Component", inversedBy="product_component")
         * @ORM\JoinColumn(name="component_id", referencedColumnName="id", nullable=false)
         */
        private $component;
    
        /**
         * @var int
         *
         * @ORM\Column(name="quantity", type="integer")
         */
        private $quantity;
    
        /**
         * @var string
         *
         * @ORM\Column(name="unit", type="string")
         */
        private $unit;
    }
    
    我得到的模式如下:

    但如果我得到产品并使用方法getComponents,我会得到空数组(),另外我会得到两个错误:

    第一:

      ...\Entity\Product    
        The mappings ...\Entity\Product#components and ...\Entity\ProductComponent#product are inconsistent with each other.
    
    第二:

    ...\Entity\ProductComponent 
    The association ...\Entity\ProductComponent#product refers to the inverse side field ...\Entity\Product#product_component which does not exist.
    The association ...\Entity\ProductComponent#component refers to the inverse side field ...\Entity\Component#product_component which does not exist.
    
    我做错了什么? 所以我将属性名更改为与映射匹配,并且我没有错误,但仍然在产品对象中没有组件

    public function testAction(Request $request, Product $product = null){
           return $this->render(
                'ProductsBundle:Product:add.html.twig', array(
                    'product' => $product,
                )
            );
        }
    
    当我在视图中转储产品时,我有一个空的
    col1:elements[]

    Product {#439 ▼
      -id: 1
      -name: "test"
      -code: "rerer"
      -category: ProductCategory {#446 ▶}
      -product_component: PersistentCollection {#462 ▼
        -snapshot: []
        -owner: Product {#439}
        -association: array:15 [ …15]
        -em: EntityManager {#91 …10}
        -backRefFieldName: "product"
        -typeClass: ClassMetadata {#444 …}
        -isDirty: false
        -initialized: false
        -coll: ArrayCollection {#463 ▼
          -elements: []
        }
      }
      -active: true
    }
    
    查看更多关于此事的信息。 在您的情况下,您有无效的引用:

    class ProductComponent
    {
        /**
         * @var int
         *
         * @ORM\Id
         * @ORM\ManyToOne(targetEntity="Product", inversedBy="components")
         * @ORM\JoinColumn(name="product_id", referencedColumnName="id", nullable=false)
         */
        private $product;
    
        /**
         * @var int
         *
         * @ORM\Id
         * @ORM\ManyToOne(targetEntity="Component", inversedBy="componentProducts")
         * @ORM\JoinColumn(name="component_id", referencedColumnName="id", nullable=false)
         */
        private $component;
    
    条令正在查找产品::$Product\u组件属性,该属性不存在。

    查看有关此问题的更多信息。 在您的情况下,您有无效的引用:

    class ProductComponent
    {
        /**
         * @var int
         *
         * @ORM\Id
         * @ORM\ManyToOne(targetEntity="Product", inversedBy="components")
         * @ORM\JoinColumn(name="product_id", referencedColumnName="id", nullable=false)
         */
        private $product;
    
        /**
         * @var int
         *
         * @ORM\Id
         * @ORM\ManyToOne(targetEntity="Component", inversedBy="componentProducts")
         * @ORM\JoinColumn(name="component_id", referencedColumnName="id", nullable=false)
         */
        private $component;
    

    Doctrine正在查找Product::$Product\u组件属性,该属性不存在。

    因此我必须创建私有$Product\u组件,并添加什么注释?不,只需像我在示例中所做的那样编辑ProductComponent反转。(并且可能阅读文档:3)好的,我这样做了,但是我应该怎么做才能得到产品对象,与产品相关的组件?所以我必须创建私有$Product_组件,我应该添加什么注释?不,只需像我在示例中那样编辑ProductComponent反转。(并且可能阅读文档:3)好的,我这样做了,但是我应该怎么做才能进入产品对象,与产品相关的组件?可能重复的可能重复的