【发布时间】:2013-12-28 21:07:27
【问题描述】:
我有一个名为 Elements 的 PHP 模型类,它有一个多对多自引用。我创建了一个这样的多对多关系:
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Elements
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Evalfor\GescompevalBundle\Entity\ElementsRepository")
*/
class Elements
{
// Constants
const COMPETENCE = "competence";
const RESULT = "result";
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
// ... More attributes ...
/**
* @ORM\ManyToMany(targetEntity="Elements", mappedBy="myElements")
**/
private $elementsWithMe;
/**
* @ORM\ManyToMany(targetEntity="Elements", inversedBy="elementsWithMe")
* @ORM\JoinTable(name="connected_elements"),
* joinColumns={@ORM\JoinColumn(name="element_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="connected_element_id", referencedColumnName="id")}
* )
**/
private $myElements;
/**
* Construct
*
*/
public function __construct() {
$this->elementsWithMe = new \Doctrine\Common\Collections\ArrayCollection();
$this->myElements = new \Doctrine\Common\Collections\ArrayCollection();
}
public function addElement(Elements $element)
{
$element->addElement($this);
$this->myElements[] = $element;
return $this;
}
public function getElements(){
return $this->myElements;
}
// ... More methods ...
}
此外,我有一个空的 Elements 存储库:
use Doctrine\ORM\EntityRepository;
class ElementsRepository extends EntityRepository
{
}
最后,我有一个元素控制器:
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Evalfor\GescompevalBundle\Entity\Elements;
use Evalfor\GescompevalBundle\Form\ElementsType;
use Symfony\Component\HttpFoundation\Request;
class ElementsController extends Controller
{
// ... Some methods ...
public function updateAction($type)
{
$em = $this->getDoctrine()->getManager();
$elements_all = $this->getDoctrine()->getRepository('EvalforGescompevalBundle:Elements')->findAll();
print_r($elements_all); exit;
// ...
}
}
问题是当我调用 findAll() 方法时浏览器会死机(对于其他类似的方法也是如此),但如果我删除 Elements 类中的所有 ManyToMany 引用,它就可以正常工作。
谁能帮我解决这个问题?提前致谢。
【问题讨论】:
-
也许在 myElements 中有一个元素。也许这会导致无限循环?有错误提示吗?
-
数据库或应用程序中没有任何关系,只有几个元素。没有错误信息,只有冻结。
标签: symfony doctrine-orm