Orm 无法确定类型为“的对象的实体标识符”;名称空间\类“;;没有匹配的字段“;id";
我正在尝试在我的Orm 无法确定类型为“的对象的实体标识符”;名称空间\类“;;没有匹配的字段“;id";,orm,doctrine-orm,laminas-api-tools,Orm,Doctrine Orm,Laminas Api Tools,我正在尝试在我的laminas api工具中使用ORM。但是,一切正常,似乎api工具hal无法识别字段 这是我的module.config.php 'router' => [ 'routes' => [ 'customer.rest.customer' => [ 'type' => 'Segment', 'options' => [ 'route' =>
laminas api工具中使用ORM
。但是,一切正常,似乎api工具hal
无法识别字段
这是我的module.config.php
'router' => [
'routes' => [
'customer.rest.customer' => [
'type' => 'Segment',
'options' => [
'route' => '/customer[/:customer_id]',
'defaults' => [
'controller' => 'Customer\\V1\\Rest\\Customer\\Controller',
],
],
],
],
'api-tools-hal' => [
'metadata_map' => [
\Customer\V1\Rest\Customer\CustomerEntity::class => [
'entity_identifier_name' => 'id',
'route_name' => 'customer.rest.customer',
'route_identifier_name' => 'customer_id',
'hydrator' => \Laminas\Hydrator\ObjectPropertyHydrator::class,
],
],
]
我的Customer\V1\Rest\Customer\CustomerEntity::class
use Doctrine\ORM\Mapping as ORM;
/**
* CustomerEntity
* @ORM\Entity
* @ORM\Table(uniqueConstraints={
* @ORM\UniqueConstraint(name="email", columns={"email"}),
* })
* @ORM\Entity(repositoryClass="Customer\V1\Rest\Customer\CustomerRepository")
* @ORM\Table(name = "customers")
*/
class CustomerEntity
{
/**
* The unique auto incremented primary key.
*
* @var int|null
*
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
ORMCLI命令ORM:schema工具:create
不起作用
但在进入domain.com/customer
时,它会抛出以下错误:
无法确定类型为的对象的实体标识符
“Customer\V1\Rest\CustomerType\CustomerEntity”;无字段
匹配“id”
当我删除实体中的ORM注释时,它就会工作
在这种情况下我需要做什么?好的,所以我需要将我的字段
公开
protected$id
变成public$id
然后它就工作了
这只是我的愚蠢。学习是好的。事实上,不,这并不愚蠢;)。请不要公开你的身份证,因为你没有理由这样做
另一种解决方法是创建两个getter/setter。在这种情况下,我猜一个getter就足够了。因此,只需创建一个公共函数getId():?int
即可修复错误,无需设置器,因为您只需要它来解析路由的参数
公共函数getId():?int
{
返回$this->id;
}
小提示:如果您使用PHPStorm,只需按Alt+Inser,然后按Getter
即可快速生成此命令;) 我有那个,那个没用。我有一个表的ID
和UUID
,我正在查询UUID
,所以基本上ID
总是私有的,UUID
是公共的。但我相信有更好的方法来做到这一点。我已经有了公众的能手和二传手。拥有公共函数getUid()
没有帮助。我真的不知道它的行为是这样的,但是当您试图修复CustomerTypeEntity
时,它谈论CustomerTypeEntity
不是很奇怪吗?对不起,我的错,错误应该是这样:无法确定类型的对象的实体标识符“Customer\V1\Rest\Customer\CustomerEntity”;没有与“id”匹配的字段。CustomerEntity
和CustomerTypeEntity
几乎完全相同。