php - Symfony 4 - Doctrine:使用 get 方法时对象未水合
问题描述
您好,我的项目 Symfony 4 有问题。我有一个包含序列号并与用户关联的验证器实体:
Validateur.php
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity(repositoryClass="App\Repository\ValidateurRepository")
*/
class Validateur
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* Ordre de validation
* @ORM\Column(type="integer")
* @Gedmo\Mapping\Annotation\SortablePosition
*/
private $ordre;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="validateurs")
* @ORM\JoinColumn(nullable=false)
*/
private $validateur;
public function getId(): ?int
{
return $this->id;
}
/**
* Retourne l'ordre de validation du validateur
*
* @return integer|null
*/
public function getOrdre(): ?int
{
return $this->ordre;
}
/**
* Insère l'ordre de validation du validateur
*
* @param integer $ordre
* @return self
*/
public function setOrdre(int $ordre): self
{
$this->ordre = $ordre;
return $this;
}
public function getValidateur(): ?User
{
return $this->validateur;
}
public function setValidateur(?User $validateur): self
{
$this->validateur = $validateur;
return $this;
}
public function __toString()
{
return $this->validateur->getFullname();
}
}
但奇怪的是,当我转储一个验证器对象时,我有信息,但在关联的用户对象中,它没有初始化,我只有用户的 ID。其他值保持为空。我的用户对象没有水合。
我注意到它几乎发生在我所有的验证器对象上。我什至会说只有我与用户连接的验证器对象,它向我发送了一个包含完整用户的验证器对象
PS:在我的用户实体中,我用这两个函数实现了 Serializable :
/**
* String representation of object
* @link http://php.net/manual/en/serializable.serialize.php
* @return string the string representation of the object or null
*/
public function serialize()
{
return serialize([
$this->id,
$this->email,
$this->password,
]);
}
/**
* Constructs the object
* @link http://php.net/manual/en/serializable.unserialize.php
* @param string $serialized <p>
* The string representation of the object.
* </p>
* @return void
*/
public function unserialize($serialized)
{
list (
$this->id,
$this->email,
$this->password,
) = unserialize($serialized);
}
解决方案
推荐阅读
- c# - 如何解决“Newtonsoft.Json.JsonSerializationException 无法找到用于类型的构造函数”Android 错误?
- reactjs - JSX标签有一个孩子,但VSCode说props中缺少属性`children`
- javascript - 在javascript中将默认日期格式更改为d/m/Y
- c# - 在单例映射管理器中多次获取变量是否比从当前对象获取变量要贵得多?
- python - 如何将 s3SummaryObject 输出到 csv 文件,每一行都在自己的 line-boto3 上
- java - 使用行 usbList.add(roots[i]); 时出现 Java 错误
- javascript - 如何正确重构和调用重构的钩子?
- c# - 我得到除 1 以外的任何数字
- java - 简单直观的理解Java程序执行
- css - 更改整个表格的字体大小