dpwbc42604 2014-01-24 17:17
浏览 28
已采纳

Doctrine2 - 使用值添加到product实体参数

Please, could you help me?

I´m looking for the best way, how to add to entity (Product) some parametrs with values.

For example: Product T-Shirt would have parametrs: size: XXL, color: red, material: cotton. How to make tables to have the best result - easy adding parametrs to product and easy filtering products by parametrs.

Thank you for your opinions.

  • 写回答

1条回答 默认 最新

  • duanjiao4763 2014-01-25 14:35
    关注

    You have two options:

    A OneToMany relationship with another entity (recommended)

    You may create a new entity called ProductProperty and declare a OneToMany relationship from Product to ProductProperty, like this:

    The Product entity

    /** 
     * @ORM\Entity
     */
    class Product
    {
    
        /**
         * @ORM\OneToMany(targetEntity="ProductProperty", mappedBy="product", cascade={"remove"})
         */
        public $properties;
    
    }
    

    The ProductProperty entity

    /** 
     * @ORM\Entity
     */
    class ProductProperty
    {
    
        /**
         * @ORM\ManyToOne(targetEntity="Product", inversedBy="properties")
         * @ORM\JoinColumn(name="product_id", referencedColumnName="id")
         */
        public $product;
    
    }
    

    Create an array property in entity Product

    Doctrine 2 supports arrays (it serializes the array into a TEXT column). Create a property that behaves like an array:

    /** 
     * @ORM\Entity
     */
    class Product
    {
    
        /** @ORM\Column(type="array") */
        public $properties;
    
        public function __construct()
        {
    
            $this->properties = []; //new PHP array notation, if using older PHP use array()
    
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题