2013-11-15 53 views
-1

在這裏我的代碼有簡單的兩個類,當我試圖通過這個插入數據。我能得到這方面的一些錯誤..錯誤gettins保存操作

頭等艙

/** 
    * @Entity @Table(name="dpf_post_template") 
    */ 
    class DpfPostTemplate 
    { 
     /** 
     * @Id @GeneratedValue @Column(type="integer") 
     * @var string 
     */ 
     private $id; 

     /** 
     * @Column(type="string") 
     * @var string 
     */ 
     private $NAME; 


     /** 
     * @Column(type="datetime") 
     */ 
     private $CDATE; 

     /** 
     * @Column(type="datetime") 
     */ 
     private $UDATE; 

     /** 
     * @Column(type="string") 
     * @var string 
     */ 
     private $CBY; 


     /** 
     * @Column(type="string") 
     * @var string 
     */ 
     private $UBY; 


     /** 
     * @Column(type="integer") 
     * @var string 
     */ 
     private $ISACTIVE; 


     /** 
     * @OneToMany(targetEntity="DpfPostTemplateAssigned", mappedBy="assignedProperty") 
     * @var asProp[] 
     */ 
     private $assignedProperties = null; 


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

     public function assignedProp($prop) 
     { 
      $this->assignedProperties[] = $prop; 
     } 

     public function getId(){ 
      return $this->id; 
     } 
     public function getName(){ 
      return $this->NAME; 
     } 
     public function setName($name){ 
      $this->NAME = $name; 
     } 
     public function setCDate(DateTime $date){ 
      $this->CDATE = $date; 
     } 
     public function setUDate(DateTime $date){ 
      $this->UDATE = $date; 
     } 
     public function setIsActive($act){ 
      $this->ISACTIVE = $act; 
     } 
     public function setCBy($cby){ 
      $this->CBY = $cby; 
     } 
     public function setUBy($uby){ 
      $this->UBY = $uby; 
     } 
    } 

二等

use Doctrine\Common\Collections\ArrayCollection; 
    /** 
    * @Entity @Table(name="dpf_post_template_assigned") 
    */ 
    class DpfPostTemplateAssigned 
    { 
     /** 
     * @Id @GeneratedValue @Column(type="integer") 
     * @var string 
     */ 
     private $id; 

     /** 
     * @Column(type="integer") 
     * @var string 
     */ 
     private $POSTID; 

     /** 
     * @Column(type="integer") 
     * @var string 
     */ 
     private $PROPID; 

     /** 
     * @Column(type="string") 
     * @var string 
     */ 
     private $TEMPLATE; 

     /** 
     * @Column(type="string") 
     * @var string 
     */ 
     private $NAME; 

     /** 
     * @Column(type="string") 
     * @var string 
     */ 
     private $SIZE; 

     /** 
     * @Column(type="integer") 
     * @var string 
     */ 
     private $ISACTIVE; 

     /** 
     * @ManyToOne(targetEntity="DpfPostTemplate", inversedBy="assignedProperties") 
     */ 
     protected $assignedProperty; 

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

     public function getName(){ 
      return $this->NAME; 
     } 
     public function setName($name){ 
      $this->NAME = $name; 
     } 
     public function setPost($post) 
     { 
      $post->assignedProp($this); 
      $this->assignedProperty = $post; 
     } 
    } 

保存操作

$post = new DpfPostTemplate(); 
    $post->setName("dinesh"); 
    $post->setCDate(new DateTime("Now")); 
    $post->setUDate(new DateTime("Now")); 
    $post->setCBy("dinesh"); 
    $post->setUBy("dinesh"); 
    $post->setIsActive(1); 
    //$em->persist($post); 
    //$em->flush(); 

    $prop = new DpfPostTemplateAssigned(); 
    $prop->setName("dinesh"); 
    $prop->setPost($post); 
    $em->persist($prop); 
    $em->flush(); 

當運行這段代碼我得到錯誤,如這.....

Fatal error: Uncaught exception 'Doctrine\ORM\ORMInvalidArgumentException' with message 'A new entity was found through the relationship 'DpfPostTemplateAssigned#assignedProperty' that was not configured to cascade persist operations for entity: [email protected] 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"}). If you cannot find out which entity causes the problem implement 'DpfPostTemplate#__toString()' to get a clue.' in C:\xampp\htdocs\dpf\vendor\doctrine\orm\lib\Doctrine\ORM\ORMInvalidArgumentException.php:91 Stack trace: #0 C:\xampp\htdocs\dpf\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php(797): Doctrine\ORM\ORMInvalidArgumentException::newEntityFoundThroughRelationship(Array, Object(DpfPostTemplate)) #1 C:\xampp\htdocs\dpf\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php(679): Doctrine\ORM\UnitOfWork->computeAssociationChanges in C:\xampp\htdocs\dpf\vendor\doctrine\orm\lib\Doctrine\ORM\ORMInvalidArgumentException.php on line 91

回答

0

該原則的對你說,你必須做的:

Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"})

你的代碼必須是這樣的:

/** 
    * @ManyToOne(targetEntity="DpfPostTemplate", inversedBy="assignedProperties", cascade={"persist", "remove"}) 
    */ 
    protected $assignedProperty; 

看了這個,請,爲獲得更多信息:Doctrine Docs

+0

Thanq其工作....但我有另一個疑問是如何插入多個DpfPostTemplateAssigned與DpfPostTemplate。請幫助我... –

+0

如果您談論ManyToMany,請嘗試閱讀以下內容:http://stackoverflow.com/questions/12743630/symfony2-doctrine-many-to-many-bidirectional-save-form-type/12744962# 12744962或給我更具體的 –