首页 > 解决方案 > 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);
}

标签: phpsymfonydoctrineserializable

解决方案


推荐阅读