下面是我的实体和表。正如您所看到的,我在表中有一个具有ManyToOne关系的父列。子行正在使用父列进行筛选。我想要获取父行或空父值。我如何在Symfony APi平台中做到这一点?
我已尝试使用自定义查询扩展。但它适用于任何情况下的条件。
# 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 AppEntity;
use ApiPlatformCoreBridgeDoctrineOrmFilterNumericFilter;
use ApiPlatformCoreAnnotationApiFilter;
use ApiPlatformCoreAnnotationApiResource;
use AppRepositoryUyapTypeRepository;
use DoctrineORMMapping as ORM;
/**
* @ApiResource(
* collectionOperations={"get", "post"},
* itemOperations={"get", "put", "delete"}
* )
* @ApiFilter(NumericFilter::class, properties={"parent.id"})
* @ORMEntity(repositoryClass=UyapTypeRepository::class)
*/
class UyapType
{
/**
* @ORMId
* @ORMGeneratedValue
* @ORMColumn(type="integer")
*/
private $id;
/**
* @ORMColumn(type="string", length=255)
*/
private $code;
/**
* @ORMColumn(type="string", length=255)
*/
private $title;
/**
* @ORMManyToOne(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-序列化组