Skip to content

Commit 3a1c032

Browse files
committed
bug fixes for enums
Signed-off-by: MarioRadu <magda_marior@yahoo.com>
1 parent b422534 commit 3a1c032

File tree

9 files changed

+20
-36
lines changed

9 files changed

+20
-36
lines changed

data/doctrine/fixtures/AdminLoader.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77
use Admin\Admin\Entity\Admin;
88
use Admin\Admin\Entity\AdminRole;
99
use Admin\Admin\Enum\AdminRoleEnum;
10-
use Admin\Admin\Repository\AdminRoleRepository;
1110
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
1211
use Doctrine\Common\DataFixtures\FixtureInterface;
1312
use Doctrine\Persistence\ObjectManager;
1413

15-
use function assert;
1614
use function password_hash;
1715

1816
use const PASSWORD_DEFAULT;
@@ -21,17 +19,14 @@ class AdminLoader implements FixtureInterface, DependentFixtureInterface
2119
{
2220
public function load(ObjectManager $manager): void
2321
{
24-
/** @var AdminRoleRepository $repository */
25-
$repository = $manager->getRepository(AdminRole::class);
26-
$superuserRole = $repository->findOneBy(['name' => AdminRoleEnum::Superuser]);
27-
assert($superuserRole instanceof AdminRole);
28-
2922
$admin = (new Admin())
3023
->setIdentity('admin')
3124
->setPassword(password_hash('dotadmin', PASSWORD_DEFAULT))
3225
->setFirstName('Dotkernel')
3326
->setLastName('Admin')
34-
->addRole($superuserRole);
27+
->addRole(
28+
$manager->getRepository(AdminRole::class)->findOneBy(['name' => AdminRoleEnum::Superuser])
29+
);
3530

3631
$manager->persist($admin);
3732
$manager->flush();

data/doctrine/migrations/Version20250307115830.php renamed to data/doctrine/migrations/Version20250310084727.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* Auto-generated Migration: Please modify to your needs!
1212
*/
13-
final class Version20250307115830 extends AbstractMigration
13+
final class Version20250310084727 extends AbstractMigration
1414
{
1515
public function getDescription(): string
1616
{
@@ -20,11 +20,11 @@ public function getDescription(): string
2020
public function up(Schema $schema): void
2121
{
2222
// this up() migration is auto-generated, please modify it to your needs
23-
$this->addSql('CREATE TABLE admin (uuid BINARY(16) NOT NULL, identity VARCHAR(100) NOT NULL, firstName VARCHAR(255) DEFAULT NULL, lastName VARCHAR(255) DEFAULT NULL, password VARCHAR(100) NOT NULL, status VARCHAR(255) DEFAULT \'active\' NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_880E0D766A95E9C4 (identity), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
23+
$this->addSql('CREATE TABLE admin (uuid BINARY(16) NOT NULL, identity VARCHAR(100) NOT NULL, firstName VARCHAR(255) DEFAULT NULL, lastName VARCHAR(255) DEFAULT NULL, password VARCHAR(100) NOT NULL, status ENUM(\'active\', \'inactive\') DEFAULT \'active\' NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_880E0D766A95E9C4 (identity), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
2424
$this->addSql('CREATE TABLE admin_roles (userUuid BINARY(16) NOT NULL, roleUuid BINARY(16) NOT NULL, INDEX IDX_1614D53DD73087E9 (userUuid), INDEX IDX_1614D53D88446210 (roleUuid), PRIMARY KEY(userUuid, roleUuid)) DEFAULT CHARACTER SET utf8mb4');
25-
$this->addSql('CREATE TABLE admin_login (uuid BINARY(16) NOT NULL, adminIp VARCHAR(50) DEFAULT NULL, country VARCHAR(50) DEFAULT NULL, continent VARCHAR(50) DEFAULT NULL, organization VARCHAR(50) DEFAULT NULL, deviceType VARCHAR(20) DEFAULT NULL, deviceBrand VARCHAR(20) DEFAULT NULL, deviceModel VARCHAR(40) DEFAULT NULL, isMobile VARCHAR(255) DEFAULT NULL, osName VARCHAR(20) DEFAULT NULL, osVersion VARCHAR(20) DEFAULT NULL, osPlatform VARCHAR(20) DEFAULT NULL, clientType VARCHAR(20) DEFAULT NULL, clientName VARCHAR(40) DEFAULT NULL, clientEngine VARCHAR(20) DEFAULT NULL, clientVersion VARCHAR(20) DEFAULT NULL, loginStatus VARCHAR(255) DEFAULT NULL, identity VARCHAR(100) DEFAULT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
26-
$this->addSql('CREATE TABLE admin_role (uuid BINARY(16) NOT NULL, name VARCHAR(255) DEFAULT \'admin\', created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
27-
$this->addSql('CREATE TABLE settings (uuid BINARY(16) NOT NULL, identifier VARCHAR(255) NOT NULL, value LONGTEXT NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, admin_uuid BINARY(16) DEFAULT NULL, INDEX IDX_E545A0C5F166D246 (admin_uuid), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
25+
$this->addSql('CREATE TABLE admin_login (uuid BINARY(16) NOT NULL, adminIp VARCHAR(50) DEFAULT NULL, country VARCHAR(50) DEFAULT NULL, continent VARCHAR(50) DEFAULT NULL, organization VARCHAR(50) DEFAULT NULL, deviceType VARCHAR(20) DEFAULT NULL, deviceBrand VARCHAR(20) DEFAULT NULL, deviceModel VARCHAR(40) DEFAULT NULL, isMobile ENUM(\'yes\', \'no\') DEFAULT NULL, osName VARCHAR(20) DEFAULT NULL, osVersion VARCHAR(20) DEFAULT NULL, osPlatform VARCHAR(20) DEFAULT NULL, clientType VARCHAR(20) DEFAULT NULL, clientName VARCHAR(40) DEFAULT NULL, clientEngine VARCHAR(20) DEFAULT NULL, clientVersion VARCHAR(20) DEFAULT NULL, loginStatus ENUM(\'success\', \'fail\') DEFAULT NULL, identity VARCHAR(100) DEFAULT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
26+
$this->addSql('CREATE TABLE admin_role (uuid BINARY(16) NOT NULL, name ENUM(\'admin\', \'superuser\') DEFAULT \'admin\', created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
27+
$this->addSql('CREATE TABLE settings (uuid BINARY(16) NOT NULL, identifier ENUM(\'table_admin_list_selected_columns\', \'table_admin_list_logins_selected_columns\') NOT NULL, value LONGTEXT NOT NULL, created DATETIME NOT NULL, updated DATETIME DEFAULT NULL, admin_uuid BINARY(16) DEFAULT NULL, INDEX IDX_E545A0C5F166D246 (admin_uuid), PRIMARY KEY(uuid)) DEFAULT CHARACTER SET utf8mb4');
2828
$this->addSql('ALTER TABLE admin_roles ADD CONSTRAINT FK_1614D53DD73087E9 FOREIGN KEY (userUuid) REFERENCES admin (uuid)');
2929
$this->addSql('ALTER TABLE admin_roles ADD CONSTRAINT FK_1614D53D88446210 FOREIGN KEY (roleUuid) REFERENCES admin_role (uuid)');
3030
$this->addSql('ALTER TABLE settings ADD CONSTRAINT FK_E545A0C5F166D246 FOREIGN KEY (admin_uuid) REFERENCES admin (uuid)');

public/images/loading-sm.gif

-2.61 KB
Loading

public/images/loading.gif

-847 Bytes
Loading

src/Admin/src/Entity/Admin.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ class Admin extends AbstractEntity implements AdminInterface
3636
#[ORM\Column(name: 'password', type: 'string', length: 100)]
3737
protected string $password;
3838

39-
#[ORM\Column(type: 'string', enumType: AdminStatusEnum::class, options: ['default' => AdminStatusEnum::Active])]
39+
#[ORM\Column(
40+
type: 'admin_status_enum',
41+
enumType: AdminStatusEnum::class,
42+
options: ['default' => AdminStatusEnum::Active]
43+
)]
4044
protected AdminStatusEnum $status = AdminStatusEnum::Active;
4145

4246
#[ORM\ManyToMany(targetEntity: AdminRole::class, fetch: 'EAGER')]
@@ -45,7 +49,7 @@ class Admin extends AbstractEntity implements AdminInterface
4549
#[ORM\InverseJoinColumn(name: 'roleUuid', referencedColumnName: 'uuid')]
4650
protected Collection $roles;
4751

48-
#[ORM\OneToMany(mappedBy: 'admin', targetEntity: Setting::class)]
52+
#[ORM\OneToMany(targetEntity: Setting::class, mappedBy: 'admin')]
4953
protected Collection $settings;
5054

5155
public function __construct()

src/Admin/src/Entity/AdminLogin.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class AdminLogin extends AbstractEntity
4040
#[ORM\Column(name: 'deviceModel', type: 'string', length: 40, nullable: true)]
4141
protected ?string $deviceModel = null;
4242

43-
#[ORM\Column(type: 'string', nullable: true, enumType: YesNoEnum::class)]
43+
#[ORM\Column(type: 'yes_no_enum', nullable: true, enumType: YesNoEnum::class)]
4444
protected ?YesNoEnum $isMobile = null;
4545

4646
#[ORM\Column(name: 'osName', type: 'string', length: 20, nullable: true)]
@@ -64,7 +64,7 @@ class AdminLogin extends AbstractEntity
6464
#[ORM\Column(name: 'clientVersion', type: 'string', length: 20, nullable: true)]
6565
protected ?string $clientVersion = null;
6666

67-
#[ORM\Column(type: 'string', nullable: true, enumType: SuccessFailureEnum::class)]
67+
#[ORM\Column(type: 'success_failure_enum', nullable: true, enumType: SuccessFailureEnum::class)]
6868
protected ?SuccessFailureEnum $loginStatus = null;
6969

7070
#[ORM\Column(name: 'identity', type: 'string', length: 100, nullable: true)]

src/Admin/src/Entity/AdminRole.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class AdminRole extends AbstractEntity
1919
use TimestampsTrait;
2020

2121
#[ORM\Column(
22-
type: 'string',
22+
type: 'admin_role_enum',
2323
nullable: true,
2424
enumType: AdminRoleEnum::class,
2525
options: ['default' => AdminRoleEnum::Admin]

src/App/src/DBAL/Types/AbstractEnumType.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@
88
use Doctrine\DBAL\Platforms\AbstractPlatform;
99
use Doctrine\DBAL\Platforms\SQLitePlatform;
1010
use Doctrine\DBAL\Types\Type;
11-
use InvalidArgumentException;
1211

1312
use function array_map;
14-
use function gettype;
1513
use function implode;
16-
use function is_object;
1714
use function sprintf;
1815

1916
abstract class AbstractEnumType extends Type
@@ -31,25 +28,13 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform): st
3128

3229
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): mixed
3330
{
34-
if ($value === null) {
35-
return null;
36-
}
37-
38-
return $this->getEnumClass()::from($value);
31+
return $value;
3932
}
4033

4134
public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): mixed
4235
{
43-
if ($value === null) {
44-
return null;
45-
}
46-
4736
if (! $value instanceof BackedEnum) {
48-
throw new InvalidArgumentException(sprintf(
49-
'Expected instance of %s, got %s',
50-
$this->getEnumClass(),
51-
is_object($value) ? $value::class : gettype($value)
52-
));
37+
return $value;
5338
}
5439

5540
return $value->value;

src/Setting/src/Entity/Setting.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Setting extends AbstractEntity
2626
#[ORM\JoinColumn(name: 'admin_uuid', referencedColumnName: 'uuid')]
2727
protected Admin $admin;
2828

29-
#[ORM\Column(type: 'string', enumType: SettingEnum::class)]
29+
#[ORM\Column(type: 'setting_enum', enumType: SettingEnum::class)]
3030
protected SettingEnum $identifier;
3131

3232
#[ORM\Column(name: "value", type: "text")]

0 commit comments

Comments
 (0)