学说双向关联错误.内存耗尽



我有两个教义实体学校和学校在zendframwork的位置

学校实体

/**
* @var schoolLocation
* 
* @ORMOneToMany(targetEntity="SchoolEntitySchoolLocation", mappedBy="school", cascade={"persist","remove"})
* 
*/
protected $schoolLocation;

学校位置实体

/**
* @var school
*
* @ORMManyToOne(targetEntity="SchoolEntitySchool", inversedBy="schoolLocation")
* @ORMJoinColumn(name="school_id", referencedColumnName="id")
*/
protected $school;

我已经展示了双向关联。现在每当我试图得到school_location

$schoolLocation = $this->entityManager->getRepository(SchoolLocation::class)->findAll();

print_r($schoolLocation)我收到以下错误消息:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 65015808 bytes) in F:projectstestmoduleSchoolsrcServiceSchoolManager.php on line 190

谁能帮我说,我在这里做错了什么?

当你在具有循环关系的对象上使用print_r时,它将继续打印关系,直到你用完内存。

class A {
public $name = "class a";
public $b;
public function __construct(B $b) {
$this->b = $b;
}
}
class B {
public $name = "class b";
public $a;
public function __construct() {
$this->a = new A($this);
}
}
print_r(new B);

此代码演示了正在发生的情况。显示的容量取决于您的 PHP 配置。

我建议使用 Xdebug 而不是打印,这有很多优点。在您的情况下,它将允许您轻松检查循环关系。

最新更新