I'm using Symfony4 with doctrine, trying to fetch my data trough my repository
I have the following query at my repository
public function findByToday($value): ?Clock
{
return $this->createQueryBuilder('c')
->select('c')
->where('c.clockIn BETWEEN :n1days AND :today')
->andWhere('c.status = 0')
->setParameter('today', date('Y-m-d h:i:s'))
->setParameter('n1days', $value)
->getQuery()
->getArrayResult();
}
My controller is
public function getToday(ClockRepository $repository)
{
$date = date('Y-m-d h:i:s', strtotime("-1 days"));
$serializer = $this->get('jms_serializer');
$clock = $repository->findByToday($date);
return new Response ($serializer->serialize($clock, "json"));
}
The response that i'm receiving is: "Return value of App\Repository\ClockRepository::findByToday() must be an instance of App\Entity\Clock or null, array returned (500 Internal Server Error)"
I tried already to send the function as findByToday(array($date)); or build a new ArrayCollection in my entity constructor.
I'm misplaced with this error.
This is my entity
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\ClockRepository")
*/
class Clock
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
public $clockIn;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
public $clockOut;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="clocks", cascade={"persist"})
*/
private $username;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $status;
public function getId(): ?int
{
return $this->id;
}
public function getClockIn(): ?\DateTimeInterface
{
return $this->clockIn;
}
public function setClockIn(?\DateTimeInterface $clockIn): self
{
$this->clockIn = $clockIn;
return $this;
}
public function getClockOut(): ?\DateTimeInterface
{
return $this->clockOut;
}
public function setClockOut(?\DateTimeInterface $clockOut): self
{
$this->clockOut = $clockOut;
return $this;
}
public function getUsername(): ?User
{
return $this->username;
}
public function setUsername(?User $username): self
{
$this->username = $username;
return $this;
}
public function getStatus(): ?bool
{
return $this->status;
}
public function setStatus(?bool $status): self
{
$this->status = $status;
return $this;
}
}
What i'm doing wrong?
Thanks for your help.