Symfony Api 平台过滤空值
Symfony Api Platform Filtering Null Values
我的实体和 table 在下方。如您所见,我在 table 中有一个具有 ManyToOne 关系的父列。
子行正在使用父列进行过滤。我想获取父行或空父值。我如何在 Symfony APi 平台中执行此操作?
我试过使用自定义查询扩展。但它适用于任何情况下的where条件。
# Table uyap_type
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| parent_id | int(11) | YES | MUL | NULL | |
| code | varchar(255) | NO | | NULL | |
| title | varchar(255) | NO | | NULL | |
+-----------+--------------+------+-----+---------+----------------+
<?php
namespace App\Entity;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\NumericFilter;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\UyapTypeRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ApiResource(
* collectionOperations={"get", "post"},
* itemOperations={"get", "put", "delete"}
* )
* @ApiFilter(NumericFilter::class, properties={"parent.id"})
* @ORM\Entity(repositoryClass=UyapTypeRepository::class)
*/
class UyapType
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $code;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @ORM\ManyToOne(targetEntity=UyapType::class)
*/
private $parent;
public function getId(): ?int
{
return $this->id;
}
public function getCode(): ?string
{
return $this->code;
}
public function setCode(string $code): self
{
$this->code = $code;
return $this;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getParent(): ?self
{
return $this->parent;
}
public function setParent(?self $parent): self
{
$this->parent = $parent;
return $this;
}
}
$repository->findBy(
['parent' => null]
);
如果你想添加带有控制器的路由,你可以配置itemOperations
https://api-platform.com/docs/core/controllers/#using-serialization-groups
我的实体和 table 在下方。如您所见,我在 table 中有一个具有 ManyToOne 关系的父列。 子行正在使用父列进行过滤。我想获取父行或空父值。我如何在 Symfony APi 平台中执行此操作?
我试过使用自定义查询扩展。但它适用于任何情况下的where条件。
# Table uyap_type
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| parent_id | int(11) | YES | MUL | NULL | |
| code | varchar(255) | NO | | NULL | |
| title | varchar(255) | NO | | NULL | |
+-----------+--------------+------+-----+---------+----------------+
<?php
namespace App\Entity;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\NumericFilter;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\UyapTypeRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ApiResource(
* collectionOperations={"get", "post"},
* itemOperations={"get", "put", "delete"}
* )
* @ApiFilter(NumericFilter::class, properties={"parent.id"})
* @ORM\Entity(repositoryClass=UyapTypeRepository::class)
*/
class UyapType
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $code;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @ORM\ManyToOne(targetEntity=UyapType::class)
*/
private $parent;
public function getId(): ?int
{
return $this->id;
}
public function getCode(): ?string
{
return $this->code;
}
public function setCode(string $code): self
{
$this->code = $code;
return $this;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getParent(): ?self
{
return $this->parent;
}
public function setParent(?self $parent): self
{
$this->parent = $parent;
return $this;
}
}
$repository->findBy(
['parent' => null]
);
如果你想添加带有控制器的路由,你可以配置itemOperations
https://api-platform.com/docs/core/controllers/#using-serialization-groups