我试图根据引用另一个实体(用户)的几种条件筛选父实体返回的集合。
我希望API平台只返回当前连接用户的消息。
这是我的实体名为File (为了全局理解非常简化)
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Serializer\Annotation\Groups;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter;
use App\Controller\FileCreatePdfController;
use Ramsey\Uuid\Uuid;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* @ApiResource(
*
* normalizationContext={"groups"={"file"},"enable_max_depth"=true},
* denormalizationContext={"groups"={"file-write"},"enable_max_depth"=true},
* attributes={"force_eager"=false},
* )
* @ApiFilter(SearchFilter::class, properties={"status": "exact"})
* @ApiFilter(DateFilter::class, properties={"updatedAt"})
* @ORM\Entity
* @ORM\Table(name="cases")
*/
class File
{
public function __construct()
{
$this->id = Uuid::uuid4();
$this->messages = new ArrayCollection();
}
public function getId()
{
return $this->id;
}
/**
* @ORM\Id
* @ORM\Column(type="uuid", unique=true)
*/
private $id;
/**
* @var Collection|FileMessage[]
*
* @Groups({"file"})
* @ORM\OneToMany(targetEntity="App\Entity\FileMessage", mappedBy="file")
*/
private $messages;
/**
* @return Collection|FileMessage[]
*/
public function getMessages(): Collection
{
return $this->messages;
}
/**
* @param FileMessage $message
* @return File
*/
public function addMessage(FileMessage $message): self
{
if (false === $this->messages->contains($message)) {
$this->messages->add($message);
}
return $this;
}
}我的文件包含了一些来自FileMessage的消息(为了全局理解非常简化)。
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Ramsey\Uuid\Uuid;
/**
* @ApiResource(
* normalizationContext={"groups"={"file-message"}, "enable_max_depth"=true},
* denormalizationContext={"groups"={"file-message-write"}, "enable_max_depth"=true},
* attributes={"force_eager"=false}
* )
* @ORM\Entity
* @ORM\Table(name="`file_messages`")
*/
class FileMessage {
/**
*
* @ORM\Id
* @ORM\Column(type="uuid", unique=true)
*/
private $id;
/**
* @var File
*
* @ORM\ManyToOne(targetEntity="File", cascade={"persist"}, inversedBy="messages")
* @Assert\Valid
* @Groups({"file-message", "file-message-write","file"})
*/
private $file;
/**
* @var User
*
* @ORM\ManyToOne(targetEntity="User", cascade={"persist"}, inversedBy="messages")
* @Assert\Valid
* @Groups({"file-message", "file-message-write","file"})
*/
private $user;
public function __construct()
{
$this->id = Uuid::uuid4();
}
public function getId()
{
return $this->id;
}
public function getFile(): ?File
{
return $this->file;
}
public function setFile(?File $file): self
{
$this->file = $file;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
}每个消息都是由一个特定的用户(另一个实体用户)发布的,我不认为发布这个实体的内容是必要的。
获取特定文件时,url/files/ file _id
显示所有用户的所有消息,我要隐藏所有不引用已连接用户的消息。

到目前为止我尝试过的解决方案:
我有什么解决办法吗?我在想也许可以用事件侦听器
发布于 2020-01-15 22:11:06
扩展是可行的。它们允许您访问,因此您可以通过调整DQL查询的WHERE子句来过滤当前资源及其关系。最好是尽早过滤数据(为了性能,还有其他原因),所以在DBMS级别这样做是最好的选择。
https://stackoverflow.com/questions/59754591
复制相似问题