2015-04-12 59 views
0

我有“项目”实体可以有几个好处,而一个好处只属于一个项目: 对我来说似乎是多对一 - 一对多的关系。 我跟着指示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可能是不正确实施,但是目前它不相关的。

我清理了效益表。

我创建了一个新的效益和附加到一个项目:

​​

的利益得到妥善保存到数据库中。它正确地链接到项目:

enter image description here

我然后注释的所有代码的控制器和简单地执行:

$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)); 

如果我倾倒$项目中,我得到:

enter image description here

当然,如果我转储$收益我得到这个:

enter image description here

回答

0

您没有在您的福利班中设置项目。

public function addBenefit(\AppBundle\Entity\Benefit $benefit) 
{ 
    $this->benefits[] = $benefit; 
    $benefit->setProject($this); // Add this 
    return $this; 
} 

注意到,我还将您的论点从收益改为收益,因为addBenefit一次处理一个收益对象。

+0

谢谢@Cerad,我错误地相信了自动获取者和设置者。这确实解决了必须手动设置两个参考的问题。我只是设置父 - >孩子,它会自动创建孩子 - >父母。我还发现,包括级联=“坚持”时,我保存父母它也保存了孩子,这是很好...但我仍然没有得到父母的所有孩子。这就像getBenefits()方法没有正确实现:( –

+0

检查你的数据库,你可能有一些老的福利记录,其project_id列为null,删除这些和所有应该前进的重要的是要明白,实体本身只是简单的php对象,并且对ORM映射一无所知,所以这取决于你确保关系设置正确,它确实有效,我保证,只需要克服学习驼峰。 ://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html – Cerad

+0

不要忘记在添加好处(或进行任何其他更改)后调用$ em-> flush()。 – Cerad