Skip to content

Commit

Permalink
Doctrine ORM 2.11: Virtual and Generated Columns:
Browse files Browse the repository at this point in the history
  • Loading branch information
SparkLee committed Feb 26, 2023
1 parent 39b490a commit cb30fde
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
47 changes: 45 additions & 2 deletions app/Domain/OrgMng/OrgType/OrgType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
}

}
3 changes: 2 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<!-- <env name="DB_CONNECTION" value="sqlite"/> -->
<env name="DB_CONNECTION" value="mysql"/>
<env name="DB_DATABASE" value="laravel"/>
<!-- <env name="DB_DATABASE" value=":memory:"/> -->
<env name="MAIL_MAILER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
Expand Down
28 changes: 28 additions & 0 deletions tests/Feature/Persistence/OrgMng/OrgTypeRepositoryDoctrineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}

0 comments on commit cb30fde

Please sign in to comment.