2013-06-19 66 views
3

我已经看过堆栈溢出和网络上其他地方的字面问题/答案,但无法找到解决此问题的办法。Symfony 2与Doctrine之间的一对多关系

在我解释这个问题,我有:

  • 剥去我的实体,从而使他们只有
  • 已清除学说查询缓存/元数据缓存
  • 降到最低的属性和方法,重新架构
  • 检查我的拼写

我有以下两个实体:

<?php 
namespace Docker\ApiBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Entity 
*/ 
class Source 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @ORM\Column(type="integer") 
    * @ORM\ManyToOne(targetEntity="Project",inversedBy="sources") 
    * @ORM\JoinColumn(referencedColumnName="id") 
    */ 
    private $project; 

} 


<?php 
namespace Docker\ApiBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Entity 
*/ 
class Project 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @ORM\OneToMany(targetEntity="Source", mappedBy="project") 
    */ 
    private $sources; 

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

    public function getSources() { 
    return $this->sources; 
    } 
} 

因此,许多'来源'可以属于一个'项目'。

在我的控制,我有:

$em = $this->getDoctrine()->getManager(); 
$project = $em->find('Docker\ApiBundle\Entity\Project', 1); 
$sources = $project->getSources()->toArray(); 

我已经试过很多东西,但我总是得到:

Notice: Undefined index: project in /.../www/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1577 

就像我说的,我知道有很多的问题,到处约这,但没有一个接受的答案解决了我的问题。

这一切对于使用Doctrine2来说都非常重要,所以不知道我做错了什么 - 它可能是非常明显的。

任何帮助,将不胜感激。

+0

$ project var not null?或者当 - > getSources() - > toArray();那崩溃? – Sybio

+0

$ project var不为空,是 - 当我在控制器中执行'$ sources = $ project-> getSources() - > toArray();'时,会导致异常。如果我拿出来,那就没问题。 – JonB

回答

5

您有:

/** 
* @ORM\Column(type="integer") 
* @ORM\ManyToOne(targetEntity="Project",inversedBy="sources") 
* @ORM\JoinColumn(referencedColumnName="id") 
*/ 
private $project; 

删除:

@ORM\Column(type="integer") 

从注释。

+0

啊哈!这就对了。它也是有意义的。谢谢。 – JonB

1

如果这正是您的代码,我没有看到您的项目类中的名称空间。尝试添加命名空间行“命名空间Docker \ ApiBundle \ Entity;”。 如果这是相同的文件夹,则不需要“使用”,但如果它是其他包或文件夹的一部分,请尝试放入一条类似“使用Docker \ ApiBundle \ Entity \ Project;”的行 在您的Source类中。我希望它可以帮助..

否则:

<?php 
namespace Azimut\ApiBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Entity 
*/ 
class Source 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    private $name; 


    /** 
    * @ORM\Column(type="integer") 
    * @ORM\ManyToOne(targetEntity="Azimut\ApiBundle\Entity\Project", inversedBy="sources") 
    * @ORM\JoinColumn(onDelete="CASCADE") 
    */ 
    private $project; 


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

/** 
* Set project 
* 
* @param integer $project 
* @return Source 
*/ 
public function setProject($project) 
{ 
    $this->project = $project; 

    return $this; 
} 

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


<?php 
namespace Azimut\ApiBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\ArrayCollection; 

/** 
* @ORM\Entity 
*/ 
class Project 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @ORM\OneToMany(targetEntity="Azimut\ApiBundle\Entity\Source", mappedBy="object", cascade={"all"}) 
    */ 
    private $sources; 

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

    public function getSources() { 
    return $this->sources; 
    } 

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

/** 
* Add sources 
* 
* @param \Azimut\ApiBundle\Entity\Source $sources 
* @return Project 
*/ 
public function addSource(\Azimut\ApiBundle\Entity\Source $sources) 
{ 
    $this->sources[] = $sources; 

    return $this; 
} 

/** 
* Remove sources 
* 
* @param \Azimut\ApiBundle\Entity\Source $sources 
*/ 
public function removeSource(\Azimut\ApiBundle\Entity\Source $sources) 
{ 
    $this->sources->removeElement($sources); 
} 
} 

和控制器的小部分: 公共职能helloAction()

{ 
     $id = 1; 
     $em = $this->getDoctrine()->getManager(); 
     $project = $em->getRepository('Azimut\ApiBundle\Entity\Project')->find($id); 

     $source1 = $em->getRepository('Azimut\ApiBundle\Entity\Source')->find(3); 
     $source2 = $em->getRepository('Azimut\ApiBundle\Entity\Source')->find(5); 
     $project->addSource($source1); 
     $sources = array(); 
     $sources = $project->getSources(); 
     var_dump($sources); 
     return .... 
} 

,这只是正常的我。

+0

好点 - 但它只是不是那里的帖子的格式。我已更新它,以便名称空间显示。 – JonB

+0

我编辑我的答案,试着让我知道。 – gerda

+0

没有运气:(同样的错误 – JonB

相关问题