symfony 4-与ManyToMany关系的语义错误

时间:2020-07-01 18:11:09

标签: symfony

我已经与Symfony一起开始了一个项目,并且是该框架的初学者。通常,我可以通过阅读论坛和文档来纠正所有错误,但是在这种情况下,我无法理解该错误。

我有一个@objc(<name>)实体,具有许多WorkingTime关系:

ManyToMany

我想在该实体的存储库中创建一个自定义查询:

<?php

namespace App\Entity;

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

/**
 * @ORM\Entity(repositoryClass=WorkingTimeRepository::class)
 */
class WorkingTime
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToMany(targetEntity=User::class, inversedBy="workingTimes")
     */
    private $user;

    /**
     * @ORM\ManyToMany(targetEntity=Customer::class, inversedBy="workingTimes")
     */
    private $customer;

    /**
     * @ORM\ManyToMany(targetEntity=Work::class, inversedBy="workingTimes")
     */
    private $work;

    /**
     * @ORM\ManyToMany(targetEntity=WorkingPlaceRelation::class, inversedBy="workingTimes")
     */
    private $workingPlace;

    /**
     * @ORM\Column(type="datetime")
     */
    private $createdAt;

    /**
     * @ORM\Column(type="integer")
     */
    private $totalTime;

    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    private $kmPerso;

    /**
     * @ORM\Column(type="boolean", nullable=true)
     */
    private $meal;

    public function __construct()
    {
        $this->user = new ArrayCollection();
        $this->customer = new ArrayCollection();
        $this->work = new ArrayCollection();
        $this->workingPlace = new ArrayCollection();
        $this->setCreatedAt(new \datetime);
    }

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

    /**
     * @return Collection|User[]
     */
    public function getUser(): Collection
    {
        return $this->user;
    }

    public function setUser(?User $user): self
    {
        $this->user = $user;

        return $this;
    }

    public function addUser(User $user): self
    {
        if (!$this->user->contains($user)) {
            $this->user[] = $user;
        }

        return $this;
    }

    public function removeUser(User $user): self
    {
        if ($this->user->contains($user)) {
            $this->user->removeElement($user);
        }

        return $this;
    }

    /**
     * @return Collection|Customer[]
     */
    public function getCustomer(): Collection
    {
        return $this->customer;
    }

    public function addCustomer(Customer $customer): self
    {
        if (!$this->customer->contains($customer)) {
            $this->customer[] = $customer;
        }

        return $this;
    }

    public function removeCustomer(Customer $customer): self
    {
        if ($this->customer->contains($customer)) {
            $this->customer->removeElement($customer);
        }

        return $this;
    }

    /**
     * @return Collection|Work[]
     */
    public function getWork(): Collection
    {
        return $this->work;
    }

    public function addWork(Work $work): self
    {
        if (!$this->work->contains($work)) {
            $this->work[] = $work;
        }

        return $this;
    }

    public function removeWork(Work $work): self
    {
        if ($this->work->contains($work)) {
            $this->work->removeElement($work);
        }

        return $this;
    }

    /**
     * @return Collection|WorkingPlaceRelation[]
     */
    public function getWorkingPlace(): Collection
    {
        return $this->workingPlace;
    }

    public function addWorkingPlace(WorkingPlaceRelation $workingPlace): self
    {
        if (!$this->workingPlace->contains($workingPlace)) {
            $this->workingPlace[] = $workingPlace;
        }

        return $this;
    }

    public function removeWorkingPlace(WorkingPlaceRelation $workingPlace): self
    {
        if ($this->workingPlace->contains($workingPlace)) {
            $this->workingPlace->removeElement($workingPlace);
        }

        return $this;
    }

    public function getCreatedAt(): ?\DateTimeInterface
    {
        return $this->createdAt;
    }

    public function setCreatedAt(\DateTimeInterface $createdAt): self
    {
        $this->createdAt = $createdAt;

        return $this;
    }

    public function getTotalTime(): ?int
    {
        return $this->totalTime;
    }

    public function setTotalTime(?int $totalTime): self
    {
        $this->totalTime = $totalTime;

        return $this;
    }

    public function getKmPerso(): ?int
    {
        return $this->kmPerso;
    }

    public function setKmPerso(?int $kmPerso): self
    {
        $this->kmPerso = $kmPerso;

        return $this;
    }

    public function getMeal(): ?int
    {
        return $this->meal;
    }

    public function setMeal(?int $meal): self
    {
        $this->meal = $meal;

        return $this;
    }
}

但是出现以下错误:

[语义错误]第0行,'name ASC'附近的col 85:错误:类App \ Entity \ WorkingTime没有名为customer.firstname的字段或关联

您可以看到我的客户实体

public function findByDate($date)
{
    return $this->createQueryBuilder('w')
        ->where('w.createdAt = :date')
        ->setParameter('date', $date)
        ->orderBy('w.customer.firstname', 'ASC')
        ->setMaxResults(10)
        ->getQuery()
        ->getResult()
    ;
}

1 个答案:

答案 0 :(得分:0)

您需要像这样加入客户:

public function findByDate($date)
{
    return $this->createQueryBuilder('w')
        ->join('w.customer', 'u')
        ->where('w.createdAt = :date')
        ->setParameter('date', $date)
        ->orderBy('u.firstname', 'ASC')
        ->setMaxResults(10)
        ->getQuery()
        ->getResult()
        ;
}