简体   繁体   中英

Return value of Myrepository must be an instance of MyEntity or null, array returned

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.

I guess was my problem.

Function in my controller

/**
 * @Rest\Get("/v1/clock/today", name="clock_today")
 */
public function getToday(ClockRepository $repository){
    $status = false;
    $user = $this->getUser();
    $date = date('Y-m-d h:i:s', strtotime("-1 days"));
    $serializer = $this->get('jms_serializer');
    $clock = $repository->findByToday($status, $date, $user);
    return new Response ($serializer->serialize($clock, "json"));
}

Query in my query builder:

public function findByToday($value1, $value2, $value3){
    return $this->createQueryBuilder('c')
        ->where('c.status = :val')
        ->andWhere('c.clockOut BETWEEN :n1days AND :today')
        ->andWhere('c.username =:user')
        ->setParameter('today', date('Y-m-d h:i:s'))
        ->setParameter('val', $value1)
        ->setParameter('n1days', $value2)
        ->setParameter('user', $value3)
        ->orderBy('c.id', 'ASC')
        ->setMaxResults(5)
        ->getQuery()
        ->getResult();
}

Works as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM