找不到异常ID,使用isInitialized false和所有属性null

我有一个Cours(本课为法语)实体,该实体具有“教授”属性,与“ Prof”(法语教师)实体具有多对多的关系。该Prof实体本身扩展了Eleve(学生)实体(教授是具有更多属性和方法的Eleve)。

为了测试我的应用程序,我创建了一个带有夹具的教授和一个教练。然后,在CourseController中,我尝试将cours和prof信息发送到视图。为此,我加载了cours,然后尝试通过cours访问我的教授。

当我尝试显示信息时,一切都很好。但是,当我尝试访问教授时,什么都没有了。我遇到一个错误,告诉我id列不存在,当我尝试转储教授时,它告诉我isInitialized为false,并且每个属性均为null。

我发现了其他有类似问题的stackoverflow(或其他论坛)主题,人们解释说symfony会延迟加载,而不会以这种方式更快地加载相关实体。但是在我看来,所有建议的解决方案都行不通或不是很好的选择。

解决方案之一似乎是将获取设置为热衷于我的多对多关系。但是问题是教授本身具有许多对码,许多关系或其他关系,并且每个实体都必须允许访问相关的实体(因此也渴望获取),因此,每次我想要获得一个实体时,我将加载整个数据库。而且我只想加载所需的内容,这将在每个控制器和每个方法中更改。

提出的另一种解决方案是调用相关对象的某些方法(在这种情况下为prof),以告诉我们需要访问该学说,以这种方式加载它。但是,此结果再次出现异常(找不到“ telling id”列)。

在CoursController中:

/**
 * @Route("/cours/{slug}",name="presentationCours")
 */
public function presentationCours(Cours $cours)
{
    $prof = $cours->getProf();
    // $prof->getLogin(); THIS result with exception

    dump($prof); // THIS shows all null values

    die();

    return $this->render('cours/presentation.html.twig',[
        "cours" => $cours,"prof" => $prof
    ]);
}

注意:如果您想知道如何在不告知symfony在何处找到$ cours var的情况下,只需在路径中插入就可以找到它。它是symfony 4中的新功能(不确定,但我认为是新功能)。我确定它可以正常工作,我做了很多次,并且转储($ cours)可以正常工作。

树枝文件:

{% extends 'base.html.twig' %}

{% block title %}{{ cours.nom }}{% endblock %}

{% block body %}

    <p>
        {{ cours.nom }}
    </p>
    <p>
        {{ cours.description }}
    </p>
    <p>
        {{ prof.login }} {# THIS result with exception #}
    </p>

{% endblock %}

Eleve实体的一部分(prof的父实体:prof扩展了eleve)

<?php
namespace App\Entity;

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

/**
 * @ORM\Entity(repositoryClass="App\Repository\EleveRepository")
 */
class Eleve
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string",length=255)
     */
    private $login;

    ...

}

教授实体的一部分:

<?php

namespace App\Entity;

use App\Entity\Eleve;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\ProfRepository")
 */
class Prof extends Eleve
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="text")
     */
    private $introduction;

    /**
     * @ORM\Column(type="text")
     */
    private $description;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Cours",mappedBy="prof")
     */
    private $cours;

cours实体的一部分:

<?php

namespace App\Entity;

use Cocur\Slugify\Slugify;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity(repositoryClass="App\Repository\CoursRepository")
 * @ORM\HasLifecycleCallbacks
 */
class Cours
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Prof",inversedBy="cours")
     * @ORM\JoinColumn(nullable=false)
     */
    private $prof;

使用phpmyadmin在数据库中:

cours表的一部分:

找不到异常ID,使用isInitialized false和所有属性null

教授表的一部分:

找不到异常ID,使用isInitialized false和所有属性null

例外(无转储):

找不到异常ID,使用isInitialized false和所有属性null

dump(prof)结果:

找不到异常ID,使用isInitialized false和所有属性null

谢谢您的时间。

编辑

问题解决了!正如jeroen所说,问题出在继承上。

这是我设法解决的问题:

<?php

namespace App\Entity;

use App\Entity\Prof;
use App\Entity\Eleve;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\InheritanceType;
use Doctrine\ORM\Mapping\DiscriminatorMap;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping\DiscriminatorColumn;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity(repositoryClass="App\Repository\EleveRepository")
 * @InheritanceType("SINGLE_TABLE")
 * @DiscriminatorColumn(name="discr",type="string")
 * @DiscriminatorMap({"eleve" = "Eleve","prof" = "Prof"})
 */
class Eleve
{

我添加了一行继承类型,discriminatorcolumn和discriminatormap。这是因为我选择了class table inheritance,但是您可以选择mapped superclass解决方案或single table inheritance解决方案。

对于那些像我一样在更改类之前进行迁移的人,在添加类表继承时遇到了问题。之后,Doctrine拒绝迁移,一个例外告诉我,这是一个外键问题。我不确定这是否是好方法,但是我删除了symfony中的迁移文件(src>迁移),将整个数据库放在phpmyadmin中,然后在CLI中使用CLI(命令行界面,在Windows和Windows上为ctrl +ù)删除了在Mac上按Ctrl +`(在键盘上Enter的左侧),我做了:

  • bin /控制台原则:数据库:创建
  • bin /控制台make:migration
  • bin /控制台学说:migrations:migrate

,现在问题已解决。但是,我真的不确定这是否是好方法,只是说它对我有用。

hanzhengli 回答:找不到异常ID,使用isInitialized false和所有属性null

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3169799.html

大家都在问