2017-03-31 82 views
0

我已经设置独立于Symfony框架的原则和Symfony形式(因为我不需要大部分)。主义试图坚持主人

我遇到的问题是,当试图坚持一个具有“类型”原则的新“审计”似乎想要坚持关系的拥有方(类型)。

例如,审计可能有一种车辆服务。

// -- 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(); 

最后窗体类是:

class AuditType extends AbstractType { 

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

一切看起来(对我来说至少)是山姆E作为所有单证都教义和Symfony的双方,但我得到这个错误:

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"})."

这实在令人沮丧,因为我不想坚持Type侧面,我只想把(在大多数基本术语)3的id到type_id列中。然而,教义似乎认为我想创造一个新的“类型”,我当然不会。它们已经存在。


使用$ entityManager->合并($审核);它部分起作用,它可以保存初始审计及其FK。但是它导致任何嵌入式表单被忽略。

+0

在你的表单生成器,尝试改变 '类型' 项** - >添加( '类型' ,EntityType :: class,array(** – ehymel

+0

实体是这样的,和EntityType ::是一样的,但是对于旧版本的PHP我没有提到,但我只限于5.3,因此symfony-form 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; 
    }