当我使用像
这样的find
方法通过id获取对象时
$manager->getRepository(Country::class)->find(1);
给定的对象看起来很奇怪,而不是字符串Id而不是int
Country {#41030 ▼
+__isInitialized__: false
-id: "1"
-name: ""
}
但是当我使用findOneBy
$manager->getRepository(Country::class)->findOneBy(['id' => 1]);
我使用正确的id类型获得相同的对象
Country {#41030 ▼
+__isInitialized__: true
-id: 1
-name: "France"
}
如果我使用另一个实体尝试find
方法,则返回的对象具有正确类型的id。
我试图找到这两个实体之间的差异而没有成功
当我在使用find
LifecycleEventArg
实体管理器的监听器中使用getEntityManager()
方法时,会导致奇怪的返回类型。
你是否有这样的案例?
修改
有Country
实体定义
/**
* @ORM\Table
* @ORM\Entity
* @DoctrineAssert\UniqueEntity("name")
*/
class Country
{
/**
* @var int
*
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(type="string", length=64, unique=true)
* @Assert\NotBlank
* @Assert\Length(max=64)
*/
private $name = '';
public function __toString(): string
{
return $this->id.' - '.$this->name;
}
public function getId(): ?int
{
return $this->id;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getName(): string
{
return $this->name;
}
}