Symfony 4 不加载相关实体
Symfony 4 not loading associated entity
这里有两个 类:
User
:
class User implements UserInterface, \Serializable
{
/**
* @var int
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Teams", inversedBy="id")
* @ORM\JoinColumn(nullable=true)
*/
private $team;
Teams
:
class Teams
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @ORM\OneToMany(targetEntity="App\Entity\User", mappedBy="team")
*/
private $id;
我正在尝试通过以下方式从我的视图中检索团队信息:dump(app.user.team)
但给我一个未加载的实体...
Teams {#450 ▼
+__isInitialized__: false
-id: 3
-name: null
-password: null
…2
}
我哪里错了?不知道怎么回事...
问候!
没问题。正如您在转储中看到的那样,团队实体的 ID 已加载。
您可以访问该对象的所有属性。
尝试 dump(app.user.team.name)
,你可以访问它。
延迟加载导致所有属性未加载
这里有两个 类:
User
:
class User implements UserInterface, \Serializable
{
/**
* @var int
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Teams", inversedBy="id")
* @ORM\JoinColumn(nullable=true)
*/
private $team;
Teams
:
class Teams
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @ORM\OneToMany(targetEntity="App\Entity\User", mappedBy="team")
*/
private $id;
我正在尝试通过以下方式从我的视图中检索团队信息:dump(app.user.team)
但给我一个未加载的实体...
Teams {#450 ▼
+__isInitialized__: false
-id: 3
-name: null
-password: null
…2
}
我哪里错了?不知道怎么回事...
问候!
没问题。正如您在转储中看到的那样,团队实体的 ID 已加载。
您可以访问该对象的所有属性。
尝试 dump(app.user.team.name)
,你可以访问它。
延迟加载导致所有属性未加载