Updated entity mappings so that schema an table prefixes can be eventually provided

This commit is contained in:
Alejandro Celaya 2020-01-29 10:53:06 +01:00
parent 96350c8b8f
commit bd2f488e2c
8 changed files with 30 additions and 15 deletions

View File

@ -8,10 +8,10 @@ use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder;
use Doctrine\ORM\Mapping\ClassMetadata;
return static function (ClassMetadata $metadata): void {
return static function (ClassMetadata $metadata, array $emConfig): void {
$builder = new ClassMetadataBuilder($metadata);
$builder->setTable('domains');
$builder->setTable(determineTableName('domains', $emConfig));
$builder->createField('id', Types::BIGINT)
->columnName('id')

View File

@ -9,10 +9,10 @@ use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder;
use Doctrine\ORM\Mapping\ClassMetadata;
use Shlinkio\Shlink\Common\Doctrine\Type\ChronosDateTimeType;
return static function (ClassMetadata $metadata): void {
return static function (ClassMetadata $metadata, array $emConfig): void {
$builder = new ClassMetadataBuilder($metadata);
$builder->setTable('short_urls')
$builder->setTable(determineTableName('short_urls', $emConfig))
->setCustomRepositoryClass(Repository\ShortUrlRepository::class);
$builder->createField('id', Types::BIGINT)
@ -57,7 +57,7 @@ return static function (ClassMetadata $metadata): void {
->build();
$builder->createManyToMany('tags', Entity\Tag::class)
->setJoinTable('short_urls_in_tags')
->setJoinTable(determineTableName('short_urls_in_tags', $emConfig))
->addInverseJoinColumn('tag_id', 'id', true, false, 'CASCADE')
->addJoinColumn('short_url_id', 'id', true, false, 'CASCADE')
->build();

View File

@ -8,10 +8,10 @@ use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder;
use Doctrine\ORM\Mapping\ClassMetadata;
return static function (ClassMetadata $metadata): void {
return static function (ClassMetadata $metadata, array $emConfig): void {
$builder = new ClassMetadataBuilder($metadata);
$builder->setTable('tags')
$builder->setTable(determineTableName('tags', $emConfig))
->setCustomRepositoryClass(Repository\TagRepository::class);
$builder->createField('id', Types::BIGINT)

View File

@ -10,10 +10,10 @@ use Doctrine\ORM\Mapping\ClassMetadata;
use Shlinkio\Shlink\Common\Doctrine\Type\ChronosDateTimeType;
use Shlinkio\Shlink\Core\Model\Visitor;
return static function (ClassMetadata $metadata): void {
return static function (ClassMetadata $metadata, array $emConfig): void {
$builder = new ClassMetadataBuilder($metadata);
$builder->setTable('visits')
$builder->setTable(determineTableName('visits', $emConfig))
->setCustomRepositoryClass(Repository\VisitRepository::class);
$builder->createField('id', Types::BIGINT)

View File

@ -8,10 +8,10 @@ use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder;
use Doctrine\ORM\Mapping\ClassMetadata;
return static function (ClassMetadata $metadata): void {
return static function (ClassMetadata $metadata, array $emConfig): void {
$builder = new ClassMetadataBuilder($metadata);
$builder->setTable('visit_locations');
$builder->setTable(determineTableName('visit_locations', $emConfig));
$builder->createField('id', Types::BIGINT)
->columnName('id')

View File

@ -8,6 +8,8 @@ use Cake\Chronos\Chronos;
use DateTimeInterface;
use PUGX\Shortid\Factory as ShortIdFactory;
use function sprintf;
function generateRandomShortCode(int $length = 5): string
{
static $shortIdFactory;
@ -39,3 +41,15 @@ function parseDateField($date): ?Chronos
return Chronos::parse($date);
}
function determineTableName(string $tableName, array $emConfig = []): string
{
$schema = $emConfig['connection']['schema'] ?? null;
// $tablePrefix = $emConfig['connection']['table_prefix'] ?? null; // TODO
if ($schema === null) {
return $tableName;
}
return sprintf('%s.%s', $schema, $tableName);
}

View File

@ -9,10 +9,12 @@ use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder;
use Doctrine\ORM\Mapping\ClassMetadata;
use Shlinkio\Shlink\Common\Doctrine\Type\ChronosDateTimeType;
return static function (ClassMetadata $metadata): void {
use function Shlinkio\Shlink\Core\determineTableName;
return static function (ClassMetadata $metadata, array $emConfig): void {
$builder = new ClassMetadataBuilder($metadata);
$builder->setTable('api_keys');
$builder->setTable(determineTableName('api_keys', $emConfig));
$builder->createField('id', Types::BIGINT)
->makePrimaryKey()
@ -31,5 +33,5 @@ return static function (ClassMetadata $metadata): void {
->build();
$builder->createField('enabled', Types::BOOLEAN)
->build();
->build();
};

View File

@ -2,6 +2,5 @@ parameters:
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
ignoreErrors:
- '#Undefined variable: \$metadata#'
- '#AbstractQuery::setParameters()#'
- '#mustRun()#'