I changed my computer and tried to import my project to my new computer.
I use Symfony (6 on my old PC and 7 on my new one). I used Doctrine to recreate the database locally before importing all data. In the project a Book has Authors and an Editor.
Why do I get error "Key "0" in object with ArrayAccess of class "Doctrine\ORM\PersistentCollection" does not exist.
?
Even though it loads all the data, the Collection of Authors is empty, and for the Editor, I have:
-lazyObjectState: Symfony\Component\VarExporter\Internal\LazyObjectState {#1038 ▼
+status: UNINITIALIZED_FULL
My relations are mapped. I don't have this problem on my old PC.
Entities:
<?php
namespace App\Entity;
use App\Repository\LibrisRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: LibrisRepository::class)]
#[ORM\Table("exlibris")]
class Libris implements \JsonSerializable
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
[...]
#[ORM\ManyToOne(inversedBy: 'libris')]
#[ORM\JoinColumn(nullable: false)]
private ?Editeur $editeur = null;
#[ORM\ManyToMany(targetEntity: Auteur::class, inversedBy: 'libris')]
private Collection $auteurs;
#[ORM\Column(length: 100, nullable: true)]
private ?string $xl_img = null;
#[ORM\Column(length: 50)]
private ?string $xl_livre = null; /*nom du Libris*/
#[ORM\OneToMany(mappedBy: 'libris', targetEntity: BDConcern::class)]
private Collection $bdConcernes;
<?php
namespace App\Entity;
use App\Repository\AuteurRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: AuteurRepository::class)]
class Auteur implements \JsonSerializable
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 30)]
private ?string $nom = null;
#[ORM\Column(length: 30, nullable: true)]
private ?string $prenom = null;
#[ORM\ManyToMany(targetEntity: Libris::class, mappedBy: 'auteurs')]
private Collection $libris;
Got the error here :
{% for l in listL %}
[...]
{{ l.auteurs[0].prenom }} {{ l.auteurs[0].nom }}</p>
[...]
{% endfor %}
AccueilController:
$listL = $libRepo->findAccRecent();
libRepo :
public function findAccRecent(): array
{
$etats = [1, 2];
return $this->createQueryBuilder('l')
->andWhere('l.xl_etat IN (:etat)')
->setParameter('etat', $etats)
->orderBy('l.xl_date', 'DESC')
->setMaxResults(18)
->getQuery()
->getResult();
}
Found out that not adding all the joins in the repository was the problem. Don't know why it worked before.