我有「項目」實體可以有幾個好處,而一個好處只屬於一個項目: 對我來說似乎是多對一 - 一對多的關係。 我跟着指示hereSymfony2多對一 - 一對多沒有連接表
項目單位是:
/**
* @ORM\Entity(repositoryClass="AppBundle\Entity\ProjectRepository")
* @ORM\Table(name="projects")
*/
class Project
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\oneToMany(targetEntity="Benefit", mappedBy="project")
*/
protected $benefits;
/**
* Constructor
*/
public function __construct()
{
$this->benefits = new \Doctrine\Common\Collections\ArrayCollection();
}
// other stuff
/**
* Add benefits
*
* @param \AppBundle\Entity\Benefit $benefits
* @return Project
*/
public function addBenefit(\AppBundle\Entity\Benefit $benefits)
{
$this->benefits[] = $benefits;
return $this;
}
/**
* Remove benefits
*
* @param \AppBundle\Entity\Benefit $benefits
*/
public function removeBenefit(\AppBundle\Entity\Benefit $benefits)
{
$this->benefits->removeElement($benefits);
}
/**
* Get benefits
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getBenefits()
{
return $this->benefits;
}
}
好處實體爲:
/**
* @ORM\Entity(repositoryClass="AppBundle\Entity\BenefitRepository")
* @ORM\Table(name="benefits")
*/
class Benefit
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
// Other relevant fields
/**
* @ORM\ManyToOne(targetEntity="Project", inversedBy="benefits")
*/
protected $project;
在我的控制,我希望做的事:
$project = $em->getRepository('AppBundle:Project')->findOneById(3);
$benefit = new Benefit();
// set some fields for the new Benefit
$benefit->setProject($project);
$em->persist($benefit);
我希望看到作爲一個集合的好處側項目實體做:
$benefits = $project->getBenefits();
但它沒有工作,所以我明確地做:
$project->addBenefit($benefit);
$em->persist($project);
$benefits = $project->getBenefits();
而且我確實看到了新的新創建的內部收益項目集合內。問題是,如果我重新運行併爲同一項目添加新的好處,我只會得到最後一個。當然,如果在代碼的相同部分中創建2個好處並添加兩個好處,我有2個集合,但這不是我想要的。在效益方面一切正常:每個新的效益都會持續存在,所有這些都正確地指向同一個項目。
我錯過了什麼?
編輯:
這裏是我做/我的東西檢查步驟:
的DB與當前實體的元數據同步。 更新項目實體是:
<?php
// src/AppBundle/Entity/Project.php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="AppBundle\Entity\ProjectRepository")
* @ORM\Table(name="projects")
*/
class Project
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\oneToMany(targetEntity="Benefit", mappedBy="project", cascade="persist")
*/
protected $benefits;
/**
* Constructor
*/
public function __construct()
{
$this->benefits = new \Doctrine\Common\Collections\ArrayCollection();
}
// Other irrelevant fields
/**
* Add benefits
*
* @param \AppBundle\Entity\Benefit $benefit
* @return Project
*/
public function addBenefit(\AppBundle\Entity\Benefit $benefit)
{
$this->benefits[] = $benefit;
$benefit->setProject($this);
return $this;
}
/**
* Remove benefits
*
* @param \AppBundle\Entity\Benefit $benefits
*/
public function removeBenefit(\AppBundle\Entity\Benefit $benefits)
{
$this->benefits->removeElement($benefits);
}
/**
* Get benefits
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getBenefits()
{
return $this->benefits;
}
}
注意,removeBenefit可能是不正確實施,但是目前它不相關的。
我清理了效益表。
我創建了一個新的效益和附加到一個項目:
的利益得到妥善保存到數據庫中。它正確地鏈接到項目:
我然後註釋的所有代碼的控制器和簡單地執行:
$em = $this->getDoctrine()->getManager();
$project = $em->getRepository('AppBundle:Project')->findOneById(3);
$benefits = $project->getBenefits();
return $this->render('testBenefits.html.twig', array(
'benefits' => $benefits, 'project' => $project));
如果我傾倒$項目中,我得到:
當然,如果我轉儲$收益我得到這個:
謝謝@Cerad,我錯誤地相信了自動獲取者和設置者。這確實解決了必須手動設置兩個參考的問題。我只是設置父 - >孩子,它會自動創建孩子 - >父母。我還發現,包括級聯=「堅持」時,我保存父母它也保存了孩子,這是很好...但我仍然沒有得到父母的所有孩子。這就像getBenefits()方法沒有正確實現:( –
檢查你的數據庫,你可能有一些老的福利記錄,其project_id列爲null,刪除這些和所有應該前進的重要的是要明白,實體本身只是簡單的php對象,並且對ORM映射一無所知,所以這取決於你確保關係設置正確,它確實有效,我保證,只需要克服學習駝峯。 ://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html – Cerad
不要忘記在添加好處(或進行任何其他更改)後調用$ em-> flush()。 – Cerad