2017-10-26 15 views
0

product_import 값을 삽입하는 방법은 무엇입니까?Doctrine이 두 엔티티를 삽입합니다.

법인은

/** 
* Features 
* 
* @ORM\Table(name="features") 
* @ORM\Entity 
*/ 
class Features 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer", nullable=false) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="IDENTITY") 
    */ 
    private $id; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="name", type="string", length=45, nullable=false) 
    */ 
    private $name; 


} 

엔티티 제품을

/** 
* Product 
* 
* @ORM\Table(name="product", indexes={@ORM\Index(name="fk_product_features1_idx", columns={"features_id"})}) 
* @ORM\Entity 
*/ 
class Product 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer", nullable=false) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="NONE") 
    */ 
    private $id; 

    /** 
    * @var \Features 
    * 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="NONE") 
    * @ORM\OneToOne(targetEntity="Features") 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="features_id", referencedColumnName="id") 
    * }) 
    */ 
    private $features; 

    /** 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * @param integer $id 
    * 
    * @return self 
    */ 
    public function setId($id) 
    { 
     $this->id = $id; 

     return $this; 
    } 

    /** 
    * @return \Features 
    */ 
    public function getFeatures() 
    { 
     return $this->features; 
    } 

    /** 
    * @param \Features $features 
    * 
    * @return self 
    */ 
    public function setFeatures(\Features $features) 
    { 
     $this->features = $features; 

     return $this; 
    } 
} 

엔티티 제품 가져 오기

/** 
* ProductImport 
* 
* @ORM\Table(name="product_import", indexes={@ORM\Index(name="fk_product_import_product1_idx", columns={"product_id", "product_features_id"})}) 
* @ORM\Entity 
*/ 
class ProductImport 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer", nullable=false) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="IDENTITY") 
    */ 
    private $id; 

    /** 
    * @var \Product 
    * 
    * @ORM\ManyToOne(targetEntity="Product") 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="product_id", referencedColumnName="id"), 
    * @ORM\JoinColumn(name="product_features_id", referencedColumnName="features_id") 
    * }) 
    */ 
    private $product; 

    /** 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * @param integer $id 
    * 
    * @return self 
    */ 
    public function setId($id) 
    { 
     $this->id = $id; 

     return $this; 
    } 

    /** 
    * @return \Product 
    */ 
    public function getProduct() 
    { 
     return $this->product; 
    } 

    /** 
    * @param \Product $product 
    * 
    * @return self 
    */ 
    public function setProduct(\Product $product) 
    { 
     $this->product = $product; 

     return $this; 
    } 
} 

INSERT

$data['product'] = entityProduto; 
$data['product_features'] = 1; 
$entity = new ProdutosImport($data); 
$em->persist($entity); 
$em->flush(); 
특징

메시지 오류

SQLSTATE [23000] : 무결성 제약 조건 위반 : 1048 이

+1

엔티티의 전체 코드에 클래스 이름을 부여하십시오. – Mcsky

+0

완료되었습니다. 더 많은 코드가 필요합니까? –

+0

ProdutosImport 엔티티 란 무엇입니까?이 데이터 배열로 생성자에서 무엇을합니까? – Mcsky

답변

0

Mcsky null 일 수 없습니다 'product_features_id'열은 entitys 하이드 레이터를 가지고,이 정보는 최대 맞지 내가하지 않았다 오류가 중요하지 않다고 생각했습니다.

public function __construct(array $data) 
    { 
     $hydrator = new ClassMethods(); 
     $hydrator->hydrate($data, $this); 
    }