0

我的目标是通过影响者 ID 获得所有应用程序。
我试过了:

public function findApplicationByInfluencer($value): ?Application
    {
        return $this->createQueryBuilder('a')
        ->where('a.influencerId = :value')
        ->setParameter('value', $value)
        ->getQuery()
        ->getResult();
    }

但是得到这个错误:“ [Semantical Error] line 0, col 47 near 'influencerId': Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected.
我不明白,也不知道如何解决。我从实体中删除了一些不影响我的问题的代码。

申请实体:

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

    /**
     * @ORM\ManyToMany(targetEntity=Influencer::class, inversedBy="applications")
     */
    private $influencerId;

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

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

    /**
     * @return Collection|Influencer[]
     */
    public function getInfluencerId(): Collection
    {
        return $this->influencerId;
    }

    public function addInfluencerId(Influencer $influencerId): self
    {
        if (!$this->influencerId->contains($influencerId)) {
            $this->influencerId[] = $influencerId;
        }

        return $this;
    }

    public function removeInfluencerId(Influencer $influencerId): self
    {
        $this->influencerId->removeElement($influencerId);

        return $this;
    }

}

影响者实体:

/**
 * @ORM\Entity(repositoryClass=InfluencerRepository::class)
 * @UniqueEntity("username", message="Ce pseudo est déjà utilisé")
 * @Vich\Uploadable
 */
class Influencer
{
    const SERVER_PATH_TO_IMAGE_FOLDER = '/public/uploads';

    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;



    /**
     * @ORM\ManyToMany(targetEntity=Application::class, mappedBy="influencerId")
     */
    private $applications;


    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $name;

    public function __construct()
    {
        $this->applications = new ArrayCollection();
        $this->brandId = new ArrayCollection();
    }

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



    /**
     * @return Collection|Application[]
     */
    public function getApplications(): Collection
    {
        return $this->applications;
    }

    public function addApplication(Application $application): self
    {
        if (!$this->applications->contains($application)) {
            $this->applications[] = $application;
            $application->addInfluencerId($this);
        }

        return $this;
    }

    public function removeApplication(Application $application): self
    {
        if ($this->applications->removeElement($application)) {
            $application->removeInfluencerId($this);
        }

        return $this;
    }
}
4

1 回答 1

0

首先,将字段和相关方法从influencerIdto重命名在语义上会更正确influencers

你有一个 ManyToMany 关系,这意味着你需要检查是否Influencerin.ArrayCollection

->where(':value in a.influencerId')

但是,您不需要运行该查询,因为该Influencer:getApplications方法已经返回了该结果。因此,这意味着您可以直接在控制器中执行以下操作:

  1. 如果您有Influencer实体:
/** @var Collection|Application[] $Applications */
$Applications = $influencer->getApplications();
  1. 如果你只有Influencer:id值:
/** @var Collection|Application[] $Applications */
$Applications = $this->getDoctrine()->getRepository(Influencer::class)->find($influencer_id)->getApplications();
于 2021-03-21T05:21:54.177 回答