John Davidson

php - Doctrine 2 Query returns class description?

0 comments
Message:


I have a small simple symfony 6.1.4 application and wanted to write a API for a few Entities, but when i fetch the data from the database (mysql) i get something what looks like a class description.


$test = $this->audienceRepository->createQueryBuilder('s')->getQuery()->getResult();


var_dump($test);


Entity


<?php

namespace App\Entity;

use App\Repository\AudienceRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: AudienceRepository::class)]
class Audience
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;

#[ORM\Column(length: 255)]
private ?string $name = null;

#[ORM\Column]
private ?bool $is_department = null;

#[ORM\Column]
private ?bool $is_company = null;

#[ORM\OneToMany(mappedBy: 'department', targetEntity: User::class)]
private Collection $users;

#[ORM\ManyToMany(targetEntity: Seminar::class, mappedBy: 'audience')]
private Collection $seminars;

public function __construct()
{
$this->users = new ArrayCollection();
$this->seminars = new ArrayCollection();
}

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

public function getName(): ?string
{
return $this->name;
}

public function setName(string $name): self
{
$this->name = $name;

return $this;
}

public function isIsDepartment(): ?bool
{
return $this->is_department;
}

public function setIsDepartment(bool $is_department): self
{
$this->is_department = $is_department;

return $this;
}

public function isIsCompany(): ?bool
{
return $this->is_company;
}

public function setIsCompany(bool $is_company): self
{
$this->is_company = $is_company;

return $this;
}

/**
* @return Collection<int, User>
*/
public function getUsers(): Collection
{
return $this->users;
}

public function addUser(User $user): self
{
if (!$this->users->contains($user)) {
$this->users->add($user);
$user->setDepartment($this);
}

return $this;
}

public function removeUser(User $user): self
{
if ($this->users->removeElement($user)) {
// set the owning side to null (unless already changed)
if ($user->getDepartment() === $this) {
$user->setDepartment(null);
}
}

return $this;
}

/**
* @return Collection<int, Seminar>
*/
public function getSeminars(): Collection
{
return $this->seminars;
}

public function addSeminar(Seminar $seminar): self
{
if (!$this->seminars->contains($seminar)) {
$this->seminars->add($seminar);
$seminar->addAudience($this);
}

return $this;
}

public function removeSeminar(Seminar $seminar): self
{
if ($this->seminars->removeElement($seminar)) {
$seminar->removeAudience($this);
}

return $this;
}
}

Repository


namespace App\Repository;
use App\Entity\Audience;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Audience>
*
* @method Audience|null find($id, $lockMode = null, $lockVersion = null)
* @method Audience|null findOneBy(array $criteria, array $orderBy = null)
* @method Audience[] findAll()
* @method Audience[] findBy(array $criteria, array $orderBy = null, $limit =null, $offset = null)
*/

class AudienceRepository extends ServiceEntityRepository {
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Audience::class);
}

public function add(Audience $entity, bool $flush = false): void
{
$this->getEntityManager()->persist($entity);

if ($flush) {
$this->getEntityManager()->flush();
}
}

public function remove(Audience $entity, bool $flush = false): void
{
$this->getEntityManager()->remove($entity);

if ($flush) {
$this->getEntityManager()->flush();
}
}

result of var_dumb


I couldnt find why doctrine is behaving this way. Does someone know why and how to prevent that?



My Answer:
The reason you are seeing the class description when you fetch data from the database using Doctrine is because you are using the `var_dump()` function to output the result. When you use `var_dump()` on an object in PHP, it will display the class name and properties of the object.

To prevent this behavior and see the actual data fetched from the database, you can use the `dump()` function provided by Symfony's VarDumper component. This function will display a more readable output of the data.

Here's how you can use `dump()` instead of `var_dump()`:

php
$test = $this->audienceRepository->createQueryBuilder('s')->getQuery()->getResult();
dump($test);


By using `dump()`, you should see a more structured and readable output of the data fetched from the database.

Rate this post

3 of 5 based on 2258 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us