findBy
from ServiceEntityRepository
returning proxies depends if i use orderBy
#11828
-
Hi, First of all, i don't know if this is a feature or a bug. So, If someone can help me understand it, I would be grateful. I have an entity called <?php
namespace App\Entity;
use Carbon\CarbonImmutable;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: InvoiceRepository::class)]
class Invoice extends InvoicingDocument
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer', options: ['unsigned' => true])]
private ?int $id = null;
#[ORM\Column(type: 'carbon_immutable_date', nullable: true)]
private ?CarbonImmutable $date = null;
#[ORM\Column(type: 'integer', nullable: true, options: ['unsigned' => true])]
private ?int $code = null;
#[ORM\Column(type: 'string', length: FieldLength::Text50)]
private string $number = '';
#[ORM\Column(type: 'utc_carbon_immutable')]
private CarbonImmutable $createdAt;
// Constructor
// Getters and setters..
} Now if i want the invoices using $this->invoiceRepository->findBy(['id' => [1,2,3,4,5,6,7,8]], ['code' => 'ASC']); The majority of values are If I don't use I know I can use IMHO using I'm missing something? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Returning proxies or not actually depends on whether the UnitOfWork of the ORM already contains a proxy for a given id. |
Beta Was this translation helpful? Give feedback.
-
One thing that could explain the usage of proxies for a different ordering might be that you have a self-referencing association in your entity (not visible in your code snippet). In this case, if your entities are only referencing entities created before them, a sort by creation date will mean that the hydration of each entity never needs to create a proxy because the related entity has already been hydrated for a previous item in the list. With a different ordering (or a sorting by descending creation date) would instead have cases where hydrating an invoice involves a related entity that is not hydrated yet (which then creates a proxy for it), even if another result item corresponds to that related entity later in the list. |
Beta Was this translation helpful? Give feedback.
One thing that could explain the usage of proxies for a different ordering might be that you have a self-referencing association in your entity (not visible in your code snippet). In this case, if your entities are only referencing entities created before them, a sort by creation date will mean that the hydration of each entity never needs to create a proxy because the related entity has already been hydrated for a previous item in the list. With a different ordering (or a sorting by descending creation date) would instead have cases where hydrating an invoice involves a related entity that is not hydrated yet (which then creates a proxy for it), even if another result item corresponds to…