2014-12-27 67 views
0

希望我的解釋是明確的,我會盡我所能:Symfony2的Form集合外鍵

我與Symfony框架工作,直到如今我得到它都解決了。那麼,我的問題是什麼?

我使用表單集合(ProjectType和DocumentType): 一個項目可以有很多文檔。

得到我所用的生成形式:CRUD命令,然後調整實體,類型等這樣的頁面:http://symfony.com/doc/current/cookbook/form/form_collections.html

這一切順利succesfull:我可以創建新的項目,並以相同的形式我可以添加許多文件。當按下提交按鈕時,數據會保存在MySQL數據庫中。

在我的教義中,我在文檔實體中創建了一個外鍵,名爲:project_id。這些關聯是正確的,因爲當我添加id到窗體下拉列表顯示與現有的項目。

但我希望該表單還可以將外鍵保留在我的文檔表中(當然這是新創建的項目PK)。所以當我用文檔創建一個新項目時,文檔的外鍵是新項目的PK。

編輯:(!只是想指出,該協會是正確的..)當我在數據庫中手動添加外鍵,然後刪除該項目與外鍵ALSE的文件被刪除

請幫助我出去了,謝謝!

----------------- ProjectController.php:

​​

-----------------項目類型.PHP:

/** 
* @param FormBuilderInterface $builder 
* @param array $options 
*/ 
public function buildForm(FormBuilderInterface $builder, array $options) { 
    $builder 
      ->add('name') 
      ->add('date_executed') 
      ->add('imageprojects', 'collection', array(
       'type'   => new DocumentType(), 
       'allow_add'  => true, 
       'allow_delete' => true, 
       'by_reference' => false 
       )) 
    ; 
} 

/** 
* @param OptionsResolverInterface $resolver 
*/ 
public function setDefaultOptions(OptionsResolverInterface $resolver) { 
    $resolver->setDefaults(array(
     'data_class' => 'Acme\DemoBundle\Entity\Project', 
     'cascade_validation' => false, 
    )); 
} 

/** 
* @return string 
*/ 
public function getName() { 
    return 'project'; 
} 

--------------- Project.php(實體):

/** 
* @ORM\Column(type="integer") 
* @ORM\Id 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
protected $id; 

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

/** 
* 
* @ORM\Column(type="date") 
*/ 
protected $date_executed; 

/** 
* 
* @ORM\Column(type="date") 
*/ 
protected $date_created; 

/** 
* @ORM\OneToMany(targetEntity="Document", mappedBy="project_id", cascade={"persist", "remove"}) 
*/ 
protected $imageprojects; 

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

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

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

/** 
* Set name 
* 
* @param string $name 
* @return Project 
*/ 
public function setName($name) 
{ 
    $this->name = $name; 

    return $this; 
} 

/** 
* Get name 
* 
* @return string 
*/ 
public function getName() 
{ 
    return $this->name; 
} 

/** 
* Set date_executed 
* 
* @param \DateTime $dateExecuted 
* @return Project 
*/ 
public function setDateExecuted($dateExecuted) 
{ 
    $this->date_executed = $dateExecuted; 

    return $this; 
} 

/** 
* Get date_executed 
* 
* @return \DateTime 
*/ 
public function getDateExecuted() 
{ 
    return $this->date_executed; 
} 

/** 
* Set date_created 
* 
* @param \DateTime $dateCreated 
* @return Project 
*/ 
public function setDateCreated($dateCreated) 
{ 
    $this->date_created = $dateCreated; 

    return $this; 
} 

/** 
* Get date_created 
* 
* @return \DateTime 
*/ 
public function getDateCreated() 
{ 
    return $this->date_created; 
} 

/** 
* Add projectimages 
* 
* @param \Acme\DemoBundle\Entity\Document $projectimages 
* @return Project 
*/ 
public function addImageproject(Document $projectimages) 
{ 
    //$this->imageprojects[] = $imageprojects; 
    $projectimages->addProjectimage($this); 

    $this->imageprojects->add($projectimages); 

    return $this; 
} 

/** 
* Remove projectimages 
* 
* @param \Acme\DemoBundle\Entity\Document $projectimages 
*/ 
public function removeImageproject(Document $projectimages) 
{ 
    $this->imageprojects->removeElement($projectimages); 
} 

/** 
* Get imageprojects 
* 
* @return \Doctrine\Common\Collections\Collection 
*/ 
public function getImageprojects() 
{ 
    return $this->imageprojects; 
} 

----------- ------- Document.php(實體)

/** 
* @ORM\Id 
* @ORM\Column(type="integer") 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
public $id; 

/** 
* @ORM\ManyToOne(targetEntity="Project", inversedBy="imageprojects") 
* @ORM\JoinColumn(name="project_id", referencedColumnName="id", onDelete="CASCADE") 
*/ 
protected $project_id; 

/** 
* @ORM\Column(type="string", length=255) 
* @Assert\NotBlank 
*/ 
public $name; 

/** 
* @ORM\Column(type="string", length=255, nullable=true) 
*/ 
public $path; 

public function getAbsolutePath() { 
    return null === $this->path ? null : $this->getUploadRootDir() . '/' . $this->id . '.' . $this->path; 
} 

public function getWebPath() { 
    return null === $this->path ? null : $this->getUploadDir() . '/' . $this->path; 
} 

protected function getUploadRootDir() { 
    // the absolute directory path where uploaded 
    // documents should be saved 
    return __DIR__ . '/../../../../web/' . $this->getUploadDir(); 
} 

protected function getUploadDir() { 
    // get rid of the __DIR__ so it doesn't screw up 
    // when displaying uploaded doc/image in the view. 
    return 'imgupload'; 
} 

/** 
* @Assert\File(maxSize="6000000") 
*/ 
private $file; 

/** 
* Sets file. 
* 
* @param UploadedFile $file 
*/ 
public function setFile(UploadedFile $file = null) { 
    $this->file = $file; 
    // check if we have an old image path 
    if (is_file($this->getAbsolutePath())) { 
     // store the old name to delete after the update 
     $this->temp = $this->getAbsolutePath(); 
    } else { 
     $this->path = 'initial'; 
    } 
} 

/** 
* Get file. 
* 
* @return UploadedFile 
*/ 
public function getFile() { 
    return $this->file; 
} 

/** 
* @ORM\PrePersist() 
* @ORM\PreUpdate() 
*/ 
public function preUpload() { 
    if (null !== $this->getFile()) { 
     $this->path = $this->getFile()->guessExtension(); 
    } 
} 

/** 
* @ORM\PostPersist() 
* @ORM\PostUpdate() 
*/ 
public function upload() { 
    if (null === $this->getFile()) { 
     return; 
    } 

    // check if we have an old image 
    if (isset($this->temp)) { 
     // delete the old image 
     unlink($this->temp); 
     // clear the temp image path 
     $this->temp = null; 
    } 

    // you must throw an exception here if the file cannot be moved 
    // so that the entity is not persisted to the database 
    // which the UploadedFile move() method does 
    $this->getFile()->move(
      $this->getUploadRootDir(), $this->id . '.' . $this->getFile()->guessExtension() 
    ); 

    $this->setFile(null); 
} 

/** 
* @ORM\PreRemove() 
*/ 
public function storeFilenameForRemove() { 
    $this->temp = $this->getAbsolutePath(); 
} 

/** 
* @ORM\PostRemove() 
*/ 
public function removeUpload() { 
    if (isset($this->temp)) { 
     unlink($this->temp); 
    } 
} 

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

/** 
* Set name 
* 
* @param string $name 
* @return Document 
*/ 
public function setName($name) { 
    $this->name = $name; 

    return $this; 
} 

/** 
* Get name 
* 
* @return string 
*/ 
public function getName() { 
    return $this->name; 
} 

/** 
* Set path 
* 
* @param string $path 
* @return Document 
*/ 
public function setPath($path) { 
    $this->path = $path; 

    return $this; 
} 

/** 
* Get path 
* 
* @return string 
*/ 
public function getPath() { 
    return $this->path; 
} 

/** 
* Add projectimages 
* 
* @param \Acme\DemoBundle\Entity\Project $projectimages 
* @return Document 
*/ 
public function addProjectimage(Project $projectimages) { 
    $this->projectimages[] = $projectimages; 
    /* 
    if (!$this->projectimages->contains($projectimages)) { 
     $this->projectimages->add($projectimages); 
    } 
    */ 
    return $this; 
} 

/** 
* Remove projectimages 
* 
* @param \Acme\DemoBundle\Entity\Project $projectimages 
*/ 
public function removeProjectimage(Project $projectimages) { 
    $this->projectimages->removeElement($projectimages); 
} 

/** 
* Get projectimages 
* 
* @return \Doctrine\Common\Collections\Collection 
*/ 
public function getProjectimages() { 
    return $this->projectimages; 
} 

/** 
* Set project_id 
* 
* @param \Acme\DemoBundle\Entity\Project $projectId 
* @return Document 
*/ 
public function setProjectId(\Acme\DemoBundle\Entity\Project $projectId = null) { 
    $this->project_id = $projectId; 

    return $this; 
} 

/** 
* Get project_id 
* 
* @return \Acme\DemoBundle\Entity\Project 
*/ 
public function getProjectId() { 
    return $this->project_id; 
} 
+0

你能告訴我們你的代碼嗎? – 2014-12-27 16:18:46

+0

我發佈了我的代碼,任何人? – 2015-01-05 11:12:09

回答