0

Studyng symfony.. I have two entities like this

Articoli entity

/**
 * @ORM\Entity(repositoryClass="App\Repository\ArticoliRepository")
 */
class Articoli
{
/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 */
private $id;

/**
 * @ORM\Column(type="string", length=255)
 */
private $nome;

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\Categorie", inversedBy="articoli")
 * @ORM\JoinColumn(nullable=false)
 */
private $categoria;

Categorie.php

/**
 * @ORM\Entity(repositoryClass="App\Repository\CategorieRepository")
 */
 class Categorie
{
/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 */
private $id;

/**
 * @ORM\Column(type="string", length=255)
 */
private $categoria;

/**
 * @ORM\OneToMany(targetEntity="App\Entity\Articoli", mappedBy="categoria", orphanRemoval=true)
 */
private $articoli;

/**
 * @ORM\Column(type="string", length=255)
 */
private $nome;

On indexController i have

        $articolo = $this->getDoctrine()->getRepository(Articoli::class)->find(201);

why $articolo categoria.categoria and categoria.articoli are null?? It should be the name of categoria

App\Entity\Articoli {#755 ▼
  -id: 201
  -nome: "Articolo #1"
  -categoria: Proxies\__CG__\App\Entity\Categorie {#742 ▼
    +__isInitialized__: false
    -id: 29
    -categoria: null
    -articoli: null
     …2
  }
}

Inspecting the profiler the executed query is

SELECT t0.id AS id_1, t0.nome AS nome_2, t0.categoria_id AS categoria_id_3 FROM articoli t0 WHERE t0.id = 201;

Should it be a join no?

thanks

Augusto Murri
  • 183
  • 1
  • 19
  • 1
    if you dump `$articolo->categoria();` what is the result? ;) – gp_sflover May 14 '20 at 15:43
  • 1
    anyway google "doctrine lazy vs eager loading" should help you, or read [What is the difference between fetch=“EAGER” and fetch=“LAZY” in doctrine](https://stackoverflow.com/questions/26891658/what-is-the-difference-between-fetch-eager-and-fetch-lazy-in-doctrine) – gp_sflover May 14 '20 at 15:53
  • 1
    fetch="EAGER" oh yeah!!!! – Augusto Murri May 14 '20 at 16:34

1 Answers1

0

Yes, you can join them or use EAGER-Property in annotation. Otherwise if you try to get the categoria, a second query will be executed with the categoria:

$articolo = $this->getDoctrine()->getRepository(Articoli::class)->find(201);
$categoria = $articolo->getCategoria();
vaion
  • 21
  • 4