Doctrine 2 Query returns class description?

Viewed 14

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?

1 Answers

Everything is correct as the doctrine returns to you Objects of your ENTITY filled with data from the DB

You get Each row as a separate class and can use it as follows

<?php 

foreach($results as $audience) {

$audience->getId();

}

Doctrine return to you list object with set data in Entity: Example

<?php 

class Audience
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = 12;

#[ORM\Column(length: 255)]
private ?string $name = "You name from DB";

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

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

#[ORM\OneToMany(mappedBy: 'department', targetEntity: User::class)]
private Collection $users = new Collection(<With Users objects>);;

#[ORM\ManyToMany(targetEntity: Seminar::class, mappedBy: 'audience')]
private Collection $seminars = new Collection(<With Seminars objects>);
Related