2017-03-31 3 views
0

Symfony 프레임 워크와 별개로 Doctrine과 Symfony-forms를 설치했습니다. (필자는 그다지 필요하지 않으므로).유지하려고하는 교리 소유자

"Type"교리가있는 새로운 "감사"를 유지하려고 할 때 관계의 소유 측면 (유형)을 유지하려는 것 같습니다.

예를 들어 Audit에는 차량 서비스 유형이있을 수 있습니다.

// -- Model/Audit.php -- 
/** 
* @var \Model\Type 
* 
* @ORM\ManyToOne(targetEntity="Model\Audit\Type", inversedBy="audits") 
* @ORM\JoinColumn(name="type_id", referencedColumnName="id", nullable=true) 
*/ 
private $type; 
/** 
* Set type 
* 
* @param \Model\Type $type 
* @return Audit 
*/ 
public function setType(\Model\Type $type) 
{ 
    $this->type = $type; 
    return $this; 
} 

그리고 반대 측 :

/** 
* @ORM\OneToMany(targetEntity="Model\Audit", mappedBy="type") 
* @var type */ 
private $audits; 

public function __construct() { 
    $this->audits = new \Doctrine\Common\Collections\ArrayCollection(); 
} 
다음

지속성 코드 같습니다

$data = $form->getData(); 
$entityManager->persist($data); 
$entityManager->flush(); 

및 마지막 Form 클래스이다

class AuditType extends AbstractType { 

    public function buildForm(FormBuilderInterface $builder, array $options) { 
     $builder 
      ->add('name') 
      ->add('type', 'entity', array(
       'class' => "Model\Type" 
      )); 
    } 

모든 보이는 (나에게 적어도) 정확하게 샘 전자 내가이 오류 받고 있어요하지만 모두 교리와 심포니 양쪽 모두 문서화 같이 : 나는 유형 측을 유지하고 싶지 않기 때문에 정말 실망

A new entity was found through the relationship 'Model\Audit#type' that was not configured to cascade persist operations for entity: Vehicle Service. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"})."

을, 난 그냥 대부분 (데려 가고 싶다는 기본 조건) 3의 ID를 type_id 열에 추가합니다. 그러나 교리는 제가 확실히하지 않는 새로운 "유형"을 만들고 싶다고 생각하는 것 같습니다. 그들은 이미 존재합니다.


$ entityManager-> merge ($ audit); 일부에서는 작동하지만, 초기 감사 및 FK가 저장 될 수 있습니다. 그러나 모든 포함 된 양식을 무시하게되었습니다. > ('형식을'추가 -

+0

을 설정 **하기 '유형'항목을 변경하려고 할 필요가 있다고 생각 , EntityType :: class, array (** – ehymel

+0

엔티티는 EntityType과 동일하지만 :: PHP의 이전 버전에서는 언급하지 않았지만 5.3으로 제한되므로 심포니 폼 2.6 .. – Doug

답변

0

나는 당신이 양식 빌더에서

/** 
* @ORM\OneToMany(targetEntity="Model\Audit", mappedBy="type") 
* @var type 
*/ 
private $audits; 

public function __construct() { 
$this->audits = new \Doctrine\Common\Collections\ArrayCollection(); 
} 

/** 
* @return ArrayCollection 
*/ 
public function getAudits() 
{ 
    return $this->audits; 
} 

/** 
* @param Audit $audit 
*/ 
public function addAudits(Audit $audit) 
{ 
    $this->audits->add($audit); 
    $audit->setTyoe($this); 
} 

및 유형에 Audit.model

 // -- Model/Audit.php -- 
    /** 
    * @var \Model\Type 
    * 
    * @ORM\ManyToOne(targetEntity="Model\Audit\Type", inversedBy="audits") 
    * @ORM\JoinColumn(name="type_id", referencedColumnName="id", nullable=true) 
    */ 
    private $type; 
    /** 
    * Set type 
    * 
    * @param \Model\Type $type 
    * @return Audit 
    */ 
    public function setType(\Model\Type $type) 
    { 
     $this->type = $type; 
    }