2016-09-10 2 views
0

아이디어는 다음과 같습니다 : 제품 간단화합물입니다. 예 제품 몇 간단한 제품으로 구성 될 수 화합물은 : 제품 Doctrine 2를 사용하여 추가 필드와 다 대다 자체 참조 연결을 만드는 방법은 무엇입니까?

있다

"칵테일" - 간단한 제품 자체의 특성 (이름, 설명, 가격 등) 및 화합물 제품 - "칵테일 분수""칵테일"을 주 성분으로 포함하는가 있습니다. 하나의 "칵테일 분수"은 50 "칵테일"으로 구성됩니다.

은 지금은 가지고있는 제품 실체가 대다 관계 와 자체 참조 : 내가 추가 필드를 를 추가 할

<?php 

namespace CT\AppBundle\Entity; 

use Doctrine\Common\Collections\ArrayCollection; 
use Doctrine\ORM\Mapping as ORM; 

/** 
* Class Product 
* 
* @ORM\Entity 
* @ORM\Table(name="products") 
*/ 
class Product 
{ 
    /** 
    * @ORM\id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    * 
    * @var int 
    */ 
    protected $id; 

    /** 
    * @ORM\Column(type="string") 
    * 
    * @var string 
    */ 
    protected $name; 

    /** 
    * @ORM\Column(
    *  name="description", 
    *  type="text" 
    *) 
    * 
    * @var string 
    */ 
    protected $desc; 

    /** 
    * @ORM\Column(type="float") 
    * 
    * @var float 
    */ 
    protected $price; 

    /** 
    * @ORM\Column(
    *  name="count_type", 
    *  type="string" 
    *) 
    * 
    * @var string 
    */ 
    protected $countType; 

    /** 
    * @ORM\Column(
    *  name="default_count", 
    *  type="integer", 
    *  options={"unsigned": true} 
    *) 
    * 
    * @var int 
    */ 
    protected $defCount; 

    /** 
    * @ORM\Column(type="boolean") 
    * 
    * @var bool 
    */ 
    protected $isCountable; 

    /** 
    * @ORM\Column(type="boolean") 
    * 
    * @var bool 
    */ 
    protected $isCompound; 

    /** 
    * @ORM\ManyToMany(
    *  targetEntity="Product", 
    *  mappedBy="components" 
    *) 
    * 
    * @var ArrayCollection 
    */ 
    private $products; 

    /** 
    * @ORM\ManyToMany(
    *  targetEntity="Product", 
    *  inversedBy="products" 
    *) 
    * @ORM\JoinTable(
    *  name="compound_products", 
    *  joinColumns={ 
    *   @ORM\JoinColumn(
    *    name="main_product_id", 
    *    referencedColumnName="id" 
    *  ) 
    *  }, 
    *  inverseJoinColumns={ 
    *   @ORM\JoinColumn(
    *    name="component_product_id", 
    *    referencedColumnName="id" 
    *  ) 
    *  } 
    *) 
    * 
    * @var ArrayCollection 
    */ 
    private $components; 

    /** 
    * Product constructor. 
    */ 
    public function __construct() 
    { 
     $this->products = new ArrayCollection(); 
     $this->components = new ArrayCollection(); 
    } 

    // Getters & setters... 
} 

compound_products 출력을 얻기 위해 하나의 복합 제품에 간단한 제품의 양을 설정하는 표 :,639,

나는 하나 개의 솔루션을 발견,하지만 난 아주 내 문제에 적용하는 방법을 이해하지 않습니다 Doctrine many to many self referencing with extra columns

당신이 관계의 많은 - 절약과 내 작업이 추가 필드를 추가 할 수있는 방법을 설명해 수 자기 참조를 가진 다 대다 (many to many)? 또는 더 나은 해결책이 있다면 그것을 공유 할 수 있습니까?

답변

1

엔티티를 연결하기 위해 별도의 엔티티를 만들어야합니다. ProductCompound와 같은 것.

그런 다음 각 관계에 대해 Product 엔터티에 두 번 연결하십시오.

/** 
* Class ProductCompound 
* 
* @ORM\Entity 
* @ORM\Table(name="compound_products") 
*/ 
class ProductCompound 
{ 
    /** 
    * @ORM\id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    * 
    * @var int 
    */ 
    protected $id; 

    /** 
    * @ORM\ManyToOne(
    *  targetEntity="Product", 
    *  inversedBy="products" 
    *) 
    * @ORM\JoinColumn(name="main_product_id", referencedColumnName="id" 
    * 
    * @var ArrayCollection 
    */ 
    protected $mainProduct; 

    /** 
    * @ORM\ManyToOne(
    *  targetEntity="Product", 
    *  inversedBy="components" 
    *) 
    * @ORM\JoinColumn(name="component_product_id", referencedColumnName="id" 
    * 
    * @var ArrayCollection 
    */ 
    protected $componentProduct; 

    /** 
    * @var double 
    * 
    * @ORM\Column(name="amount", type="float", nullable=true) 
    */ 
    protected $amount; 

그리고 수정 제품 : 조언을

/** 
    * Class Product 
    * 
    * @ORM\Entity 
    * @ORM\Table(name="products") 
    */ 
    class Product 
    { 
... 

    /** 
    * @ORM\OneToMany(
    *  targetEntity="ProductCompound", 
    *  mappedBy="mainProduct" 
    *) 
    * 
    * @var ArrayCollection 
    */ 
    private $productLinks; 

    /** 
    * @ORM\OneToMany(
    *  targetEntity="ProductCompound", 
    *  mappedBy="componentProduct" 
    *) 
    * 
    * @var ArrayCollection 
    */ 
    private $componentLinks; 
+0

감사) 나는 동일한 솔루션에 대해 생각했다. –