Max P.
Max P.

Reputation: 5679

Doctrine rewrites relationship with eager fetching value after findBy

I have a simple entity

/**
 * @ORM\Entity(repositoryClass="...\Repository\UserTestRepository")
 * @ORM\Table(name="users", uniqueConstraints={
 *     @ORM\UniqueConstraint(name="U_email", columns={"email"})
 * })
 * @UniqueEntity("email", message="Email is already used!")
 */
class UserTest
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     * @ORM\Column(type="string", length=180)
     * @Assert\NotBlank()
     * @Assert\Email()
     */
    protected $email;

    /**
     * @var string
     * @ORM\Column(type="string", length=255, nullable=true)
     * @Assert\NotBlank()
     */
    protected $jobTitle;

    /**
     * @var Company
     * @ORM\ManyToOne(targetEntity="...\Entity\Company", fetch="EAGER")
     * @ORM\JoinColumn(name="company_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
     */
    private $company;

    public function getId()
    {
        return $this->id;
    }

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

    public function getJobTitle()
    {
        return $this->jobTitle;
    }

    public function setJobTitle($jobTitle)
    {
        $this->jobTitle = $jobTitle;
        return $this;
    }

    public function setCompany(...\Entity\Company $company = null)
    {
        $this->company = $company;

        return $this;
    }

    public function getCompany()
    {
        return $this->company;
    }
}

and controller

$repo = $this->getDoctrine()->getRepository('..\Entity\UserTest');

$user = $repo->find(519);
dump($user);

$user->setJobTitle('new value');
$user->setCompany(null);
dump($user);

$repo->findBy(['email' => '[email protected]']);
dump($user);

1-st dump, original

2-nd dump, after changes without flush

3-rd dump, after findBy, jobTitle - with new value, company - original value, changes were lost

1-st dump, original

2-nd dump, after changes without flush

3-rd dump, after findBy, jobTitle - with new value, company - original value, changes were lost

Is it normal doctrine behaviour or is it a bug? I use doctrine/orm v2.5.11. Was it fixed in newer versions?

Any help, pls

Upvotes: 4

Views: 106

Answers (1)

Valentin Knyazev
Valentin Knyazev

Reputation: 166

It's Doctrine normal behaviour when looking for unflushed entity through repository's findBy(). Please refer to https://github.com/doctrine/orm/issues/5092 to get more info.

Upvotes: 1

Related Questions