2012-05-16 3 views
4

아래에 일대 다 양방향 관계가 있습니다.doctrine2 : 일대 다 양방향 관계에서 역변환을 저장하는 방법은 무엇입니까?

나는 새/편집 카테고리 형태의 카테고리에 관련된 제품을 저장하려고 할 때 symfony2 작업으로 침전물 작업을 생성 한 후, 제품이 저장되지 않습니다 ...

namespace Prueba\FrontendBundle\Entity; 

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

/** 
* @ORM\Entity 
* @ORM\Table(name="category") 
*/ 
class Category 
{ 

    /** 
    * @var integer $id 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @ORM\OneToMany(targetEntity="Product", mappedBy="category") 
    */ 
    protected $products; 

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

    public function __construct() 
    { 
     $this->products = new ArrayCollection(); 
    } 

    public function getId() 
    { 
     return $this->id; 
    } 

    public function getName() 
    { 
     return $this->name; 
    } 

    public function setName($name) 
    { 
     $this->name = $name; 
    } 

    public function __toString() 
    { 
     return $this->name; 
    } 

    public function getProducts() 
    { 
     return $this->products; 
    } 

    public function setProducts($products) 
    { 
     die("fasdf"); //here is not entering 
     $this->products[] = $products; 
    } 

    public function addProduct($product) 
    { 
     die("rwerwe"); //here is not entering 
     $this->products[] = $product; 
    } 
} 
namespace Prueba\FrontendBundle\Entity; 

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

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

    /** 
    * @ORM\ManyToOne(targetEntity="Category", inversedBy="products") 
    * @ORM\JoinColumn(name="category_id", referencedColumnName="id") 
    */ 
    protected $category; 

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


    public function getId() 
    { 
     return $this->id; 
    } 

    public function getName() 
    { 
     return $this->name; 
    } 

    public function setName($name) 
    { 
     $this->name = $name; 
    } 

    public function getCategory() 
    { 
     return $this->category; 
    } 

    public function setCategory($category) 
    { 
     $this->category = $category; 
    } 

    public function __toString() 
    { 
     return $this->name; 
    } 
} 

답변

8

양방향이므로 양쪽에서 연결을 업데이트해야합니다.

public function setProductCategory($product_category) 
{ 
    $this->productCategory = $product_category; 
    $product_category->addProduct($this); 
} 

팁 : 내 청춘의 사용 동시에 두 단체를 업데이트 한 후

public function addProduct($product) 
{ 
    $this->children->add($product); 
} 

을 그리고 :

카테고리 법인에이 기능을 추가 (당신은 당신이 좋아하는 경우가하면 addChild 호출 할 수 있습니다) 엔티티를 설명하는 $ children/$ parent. $ category/$ product라고 부르면 다른 "부모"관계를 추가 할 때 문제가 발생합니다.

+0

물론 "addProduct"가 $ product-> setProductCategory ($ this)를 실행하고 좋아한다면 다른 방법으로도 할 수 있습니다. 작업 환경에 따라 다릅니다. –

+0

내 질문을 만들었을 때 적절한 수업을 붙여 넣지 않았어. 이제 괜찮아. 다시 봐 줄래? – ziiweb

+0

똑같은 문제가 적용됩니다. 양쪽에서 양방향 업데이트가 필요합니다. Category :: addProduct ($ product)에 $ product-> setCategory ($ this) 줄을 추가하십시오. 너는 설정되어야한다. –

관련 문제