I'm running two equal find methods. One is returning a proxy, the other one is returning a real Entity object.
I can't find a reason why doctrine would handle both differently. Do you have any idea why my first Entity is returned as a proxy?
This project is running:
"doctrine/doctrine-bundle": "^2.6",
"symfony/symfony": "5.4.*",
Following my Test Controller Action:
public function startAction(InstitutionRepository $institutionRepository, CountryRepository $countryRepository): Response
{
$result = $institutionRepository->find(266);
$result2 = $countryRepository->find(1);
return $this->json([$result, $result2]);
}
None of the Entity classes have black magic inside and look equal:
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Institutions.
*
* @ORM\Table(name="institutions")
*
* @ORM\Entity(repositoryClass="App\Repository\InstitutionRepository")
*/
class Institution
{
/**
* @var int
*
* @ORM\Column(name="institution", type="integer", nullable=false)
*
* @ORM\Id
*
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="code", type="string", length=10, nullable=false)
*/
private $code;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
public function getId(): int
{
return $this->id;
}
public function setId(int $id): void
{
$this->id = $id;
}
public function getCode(): string
{
return $this->code;
}
public function setCode(string $code): void
{
$this->code = $code;
}
public function getName(): string
{
return $this->name;
}
public function setName(string $name): void
{
$this->name = $name;
}
}
Country Entity:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Countries.
*
* @ORM\Table(name="countries", uniqueConstraints={@ORM\UniqueConstraint(name="code", columns={"code"})})
*
* @ORM\Entity(repositoryClass="App\Repository\CountryRepository")
*/
class Country
{
/**
* @var int
*
* @ORM\Column(name="country", type="integer", nullable=false)
*
* @ORM\Id
*
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="code", type="string", length=10, nullable=false)
*/
private $code;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=45, nullable=false)
*/
private $name;
/**
* Get id.
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set code.
*
* @param string $code
*
* @return Country
*/
public function setCode($code)
{
$this->code = $code;
return $this;
}
/**
* Get code.
*
* @return string
*/
public function getCode()
{
return $this->code;
}
/**
* Set name.
*
* @param string $name
*
* @return Country
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name.
*
* @return string
*/
public function getName()
{
return $this->name;
}
}
And both Repository methods are almost empty:
<?php
declare(strict_types=1);
namespace App\Repository;
use App\Entity\Country;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Country>
*/
class CountryRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Country::class);
}
}
InstitutionRepository:
<?php
declare(strict_types=1);
namespace App\Repository;
use Database\OrganisationBundle\Entity\Institution;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Country>
*/
class InstitutionRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Institution::class);
}
}
The serialized Result looks like this:
[
{
"id": 266,
"code": "LGSE_QS_IN",
"name": "LGSE QS Test Institution",
"__initializer__": null,
"__cloner__": null,
"__isInitialized__": true
},
{
"id": 1,
"code": "AF",
"name": "Afghanistan"
}
]
I'm totally out of ideas. Maybe someone experienced something like this already with Symfony and Doctrine?
There is a mistake in your InstitutionRepository.
should be