// HogeTable.php
public function beforeSave(EventInterface $event, EntityInterface $entity, ArrayObject $options)
{
if ($entity->isNew()) {
$entity->id = Text::uuid();
}
}
// HogeTableTest.php
public function testIdAutoInsert(): void
{
$this->assertCount(
0,
$this->Users->find()->all(),
'usersテーブルに一件もレコードが存在していないこと'
);
$userEntity = $this->Users->newEntity([
'password' => 'p@ssw0rd:::::012345',
'email' => 'example@example.com',
]);
$this->Users->saveOrFail($userEntity);
$actual = $this->Users->find()->firstOrFail();
$this->assertNotNull($actual->id);
$this->assertEquals(36, strlen($actual->id));
}