From cb30fdeef5e0732b54067365a6abf3f2d3b5d3d0 Mon Sep 17 00:00:00 2001 From: SparkLee Date: Sun, 26 Feb 2023 21:34:22 +0800 Subject: [PATCH] =?UTF-8?q?Doctrine=20ORM=202.11:=20Virtual=20and=20Genera?= =?UTF-8?q?ted=20Columns=EF=BC=9A=20*=20https://www.doctrine-project.org/2?= =?UTF-8?q?022/01/11/orm-2.11.html?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Domain/OrgMng/OrgType/OrgType.php | 47 ++++++++++++++++++- phpunit.xml | 3 +- .../OrgMng/OrgTypeRepositoryDoctrineTest.php | 28 +++++++++++ 3 files changed, 75 insertions(+), 3 deletions(-) diff --git a/app/Domain/OrgMng/OrgType/OrgType.php b/app/Domain/OrgMng/OrgType/OrgType.php index 06cf816..97f49b3 100644 --- a/app/Domain/OrgMng/OrgType/OrgType.php +++ b/app/Domain/OrgMng/OrgType/OrgType.php @@ -3,6 +3,7 @@ namespace App\Domain\OrgMng\OrgType; use App\Domain\TenantMng\Tenant; +use DateTimeImmutable; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping\Column; use Doctrine\ORM\Mapping\Entity; @@ -21,8 +22,23 @@ class OrgType #[Column(type: Types::STRING, length: 10)] private string $name; - #[Column(type: Types::INTEGER, enumType: OrgTypeStatus::class)] - private OrgTypeStatus $status = OrgTypeStatus::Effective; + #[Column] private OrgTypeStatus $status = OrgTypeStatus::Effective; + + // MySQL 和 SQLite 都支持:columnDefinition: "TIMESTAMP DEFAULT CURRENT_TIMESTAMP" + #[Column(type: Types::DATETIME_IMMUTABLE, + insertable: false, + updatable: false, + columnDefinition: "TIMESTAMP DEFAULT CURRENT_TIMESTAMP", + generated: "ALWAYS")] + private DateTimeImmutable $CreatedAt; + + // 只有 MySQL 支持,SQLite 是不支持的!columnDefinition: "TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP" + #[Column(type: Types::DATETIME_IMMUTABLE, + insertable: false, + updatable: false, + columnDefinition: "TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP", + generated: "ALWAYS")] + private DateTimeImmutable $lastUpdatedAt; /** * @param string $code @@ -61,4 +77,31 @@ public function getName(): string { return $this->name; } + + /** + * @param string $name + * @return OrgType + */ + public function setName(string $name): OrgType + { + $this->name = $name; + return $this; + } + + /** + * @return mixed + */ + public function getCreatedAt() + { + return $this->CreatedAt; + } + + /** + * @return mixed + */ + public function getLastUpdatedAt() + { + return $this->lastUpdatedAt; + } + } diff --git a/phpunit.xml b/phpunit.xml index 2ac86a1..310a208 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -21,7 +21,8 @@ - + + diff --git a/tests/Feature/Persistence/OrgMng/OrgTypeRepositoryDoctrineTest.php b/tests/Feature/Persistence/OrgMng/OrgTypeRepositoryDoctrineTest.php index 8bd2bc7..16e01df 100644 --- a/tests/Feature/Persistence/OrgMng/OrgTypeRepositoryDoctrineTest.php +++ b/tests/Feature/Persistence/OrgMng/OrgTypeRepositoryDoctrineTest.php @@ -35,4 +35,32 @@ public function test_should_return_true_if_record_exists_with_given_code_and_sta self::assertTrue($this->repository->existsByCodeAndStatus($tenant, 'DEVCENT', OrgTypeStatus::Effective->value)); self::assertTrue($this->repository->existsByCodeAndStatus($tenant->getId(), 'DEVCENT', OrgTypeStatus::Effective)); } + + /** + * @see https://www.doctrine-project.org/2022/01/11/orm-2.11.html + */ + public function test_virtual_and_generated_columns() + { + // 1、新增 + $tenant = new Tenant(); + $orgType = new OrgType('DEVCENT', $tenant, 'foo', OrgTypeStatus::Effective); + EntityManager::persist($tenant); + EntityManager::persist($orgType); + EntityManager::flush(); + $createdAt1 = $orgType->getCreatedAt(); + $lastUpdatedAt1 = $orgType->getLastUpdatedAt(); + + // 2、等待一秒 + sleep(1); + + // 3、更新 + $orgType->setName('bar'); + EntityManager::flush(); + $createdAt2 = $orgType->getCreatedAt(); + $lastUpdatedAt2 = $orgType->getLastUpdatedAt(); + + // 4、更新记录后,创建时间不变,更新时间自动变更 + self::assertSame($createdAt1->getTimestamp(), $createdAt2->getTimestamp()); + self::assertThat($lastUpdatedAt2->getTimestamp(), self::greaterThan($lastUpdatedAt1->getTimestamp())); + } }