只有 POST 个值为 true 的元素
Only POST elements with value=true
我正在使用 Api-platform 作为项目的后端。它从名为 "Voiture" 的 class 发送数据。但是我不需要 "Voiture" 的所有元素,这个 class 有一个名为 Parked 的布尔元素。
而且我只想发送 Parked= true 的元素。
是否可以这样做,我认为从接收方过滤数据不切实际。
这是我的 "Voiture" class:
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
/**
* @ApiResource(
* attributes={"order"={"gareele": "DESC"}}
* )
* @ORM\Entity(repositoryClass="App\Repository\VoitureRepository")
* @ApiFilter(
* SearchFilter::class,
* properties={
* "matricule": "partial"
* }
* )
*/
class Voiture
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @Groups({"toute"})
*/
private $id;
/**
* @ORM\Column(type="string", length=200)
* @Groups({"toute"})
*/
private $matricule;
/**
* @ORM\Column(type="boolean")
* @Groups({"toute"})
*/
private $parked;
public function getId(): ?int
{
return $this->id;
}
public function getMatricule(): ?string
{
return $this->matricule;
}
public function setMatricule(string $matricule): self
{
$this->matricule = $matricule;
return $this;
}
public function getParked(): ?bool
{
return $this->parked;
}
public function setParked(bool $parked): self
{
$this->parked = $parked;
return $this;
}
}
是的,存储库具有按任何条件查找实体的方法。您将需要在您的控制器中使用如下内容:
use App\Entity\Voiture;
...
$repository = $this->getDoctrine()->getRepository(Voiture::class);
$parkedVoitures = $repository->findBy(
['parked' => true]
);
$parkedVoituresOrderedByMatricule = $repository->findBy(
['parked' => true],
['matricule' => 'ASC']
);
https://symfony.com/doc/current/doctrine.html#fetching-objects-from-the-database
也许您正在寻找个性化注释,我正在使用 api-platform,特别是 GraphQL,所以,我不需要所有行,因为,我有一个逻辑删除,没有物理删除。我们不是制定业务规则,任何开发人员都对其进行自己的解释,而是进行个人注释。
看到这个link,这个link也适用于SF4。
所以我终于找到了一个解决方案,原来有一个注释 Api 过滤布尔数据的平台。
我刚刚将下面的注释添加到我的 class 中,当我将 ?parked=true 添加到我的地址时它起作用了,它工作正常。
* @ApiFilter(BooleanFilter::class,
* properties={
* "parked"
* }
* )
我正在使用 Api-platform 作为项目的后端。它从名为 "Voiture" 的 class 发送数据。但是我不需要 "Voiture" 的所有元素,这个 class 有一个名为 Parked 的布尔元素。
而且我只想发送 Parked= true 的元素。 是否可以这样做,我认为从接收方过滤数据不切实际。
这是我的 "Voiture" class:
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
/**
* @ApiResource(
* attributes={"order"={"gareele": "DESC"}}
* )
* @ORM\Entity(repositoryClass="App\Repository\VoitureRepository")
* @ApiFilter(
* SearchFilter::class,
* properties={
* "matricule": "partial"
* }
* )
*/
class Voiture
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @Groups({"toute"})
*/
private $id;
/**
* @ORM\Column(type="string", length=200)
* @Groups({"toute"})
*/
private $matricule;
/**
* @ORM\Column(type="boolean")
* @Groups({"toute"})
*/
private $parked;
public function getId(): ?int
{
return $this->id;
}
public function getMatricule(): ?string
{
return $this->matricule;
}
public function setMatricule(string $matricule): self
{
$this->matricule = $matricule;
return $this;
}
public function getParked(): ?bool
{
return $this->parked;
}
public function setParked(bool $parked): self
{
$this->parked = $parked;
return $this;
}
}
是的,存储库具有按任何条件查找实体的方法。您将需要在您的控制器中使用如下内容:
use App\Entity\Voiture;
...
$repository = $this->getDoctrine()->getRepository(Voiture::class);
$parkedVoitures = $repository->findBy(
['parked' => true]
);
$parkedVoituresOrderedByMatricule = $repository->findBy(
['parked' => true],
['matricule' => 'ASC']
);
https://symfony.com/doc/current/doctrine.html#fetching-objects-from-the-database
也许您正在寻找个性化注释,我正在使用 api-platform,特别是 GraphQL,所以,我不需要所有行,因为,我有一个逻辑删除,没有物理删除。我们不是制定业务规则,任何开发人员都对其进行自己的解释,而是进行个人注释。
看到这个link,这个link也适用于SF4。
所以我终于找到了一个解决方案,原来有一个注释 Api 过滤布尔数据的平台。 我刚刚将下面的注释添加到我的 class 中,当我将 ?parked=true 添加到我的地址时它起作用了,它工作正常。
* @ApiFilter(BooleanFilter::class,
* properties={
* "parked"
* }
* )