步骤1:在实体类中使用@Id注释来标识新的标识符字段,如下所示:
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class MyEntity
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string")
*/
private $newIdentifier;
}
步骤2:在该实体的Schema.yaml中定义新的标识符字段,如下所示:
MyEntity:
identifier: newIdentifier
步骤3:更新您的API平台配置(api_platform.yaml)。确保在这里引用的模式与上面的模式相同。
api_platform:
mapping:
paths:
- '%kernel.project_dir%/src/Entity'
# 在这里引用您的模式。
# 如果没有,只需将其添加到“types”部分中!
# 注意:也可以使用FOSRestBundle来设置您的API!
types:
-
name: 'MyEntity'
# 引用您的模式文件。
file: 'Entity/Schema.yaml'
现在,您可以使用新的标识符字段来引用实体:
http://yourapi.com/myentities/{newIdentifier}
以上就是将“ApiPlatform - 更改实体的标识符”进行的重新编写后的解决方法。