具有额外字段

时间:2016-03-10 13:08:44

标签: php symfony doctrine-orm doctrine

我有三个班级:

  1. 产品
  2. 组件
  3. ProductComponent
  4. 我想用额外的字段(组件数量)创建多对多的关系,产品是由组件构建的,所以我创建了三个类:

    产品:

    /**
     * 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:

    /**
     * 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;
    }
    

    我得到的结构如this

    但如果我得到Product并使用方法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
    }
    

1 个答案:

答案 0 :(得分:0)

检查The docs on associations以获取有关此事的更多信息。 在您的情况下,您有无效的引用:

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_component属性,该属性不存在。