From 1c542e34556aca00dfe6e897f9407ca126b15a7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20=C3=81lvarez?= Date: Mon, 23 May 2022 15:34:24 +0200 Subject: [PATCH] dates --- migrations/Version20220523132530.php | 47 +++++++++++++++++++++++++ src/Controller/IndexController.php | 2 +- src/DataFixtures/MerFixtures.php | 2 +- src/Entity/MerParaSub.php | 30 ++++++++++++++++ src/Repository/MerParaSubRepository.php | 12 ++++++- 5 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 migrations/Version20220523132530.php diff --git a/migrations/Version20220523132530.php b/migrations/Version20220523132530.php new file mode 100644 index 0000000..55178a3 --- /dev/null +++ b/migrations/Version20220523132530.php @@ -0,0 +1,47 @@ +addSql('DROP INDEX IDX_E4F82A3DED5CA9E6'); + $this->addSql('CREATE TEMPORARY TABLE __temp__mer_para_sub AS SELECT id, service_id, red_code FROM mer_para_sub'); + $this->addSql('DROP TABLE mer_para_sub'); + $this->addSql('CREATE TABLE mer_para_sub (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, service_id INTEGER NOT NULL, red_code VARCHAR(255) DEFAULT NULL, sub_date DATETIME NOT NULL --(DC2Type:datetime_immutable) + , unsub_date DATETIME DEFAULT NULL --(DC2Type:datetime_immutable) + , CONSTRAINT FK_E4F82A3DED5CA9E6 FOREIGN KEY (service_id) REFERENCES mer_service (id) NOT DEFERRABLE INITIALLY IMMEDIATE)'); + $this->addSql('INSERT INTO mer_para_sub (id, service_id, red_code, sub_date) SELECT id, service_id, red_code, DATETIME() FROM __temp__mer_para_sub'); + $this->addSql('DROP TABLE __temp__mer_para_sub'); + $this->addSql('CREATE INDEX IDX_E4F82A3DED5CA9E6 ON mer_para_sub (service_id)'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('DROP INDEX IDX_E4F82A3DED5CA9E6'); + $this->addSql('CREATE TEMPORARY TABLE __temp__mer_para_sub AS SELECT id, service_id, red_code FROM mer_para_sub'); + $this->addSql('DROP TABLE mer_para_sub'); + $this->addSql('CREATE TABLE mer_para_sub (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, service_id INTEGER NOT NULL, red_code VARCHAR(255) DEFAULT NULL)'); + $this->addSql('INSERT INTO mer_para_sub (id, service_id, red_code) SELECT id, service_id, red_code FROM __temp__mer_para_sub'); + $this->addSql('DROP TABLE __temp__mer_para_sub'); + $this->addSql('CREATE INDEX IDX_E4F82A3DED5CA9E6 ON mer_para_sub (service_id)'); + } +} diff --git a/src/Controller/IndexController.php b/src/Controller/IndexController.php index b903c4b..0eb1cbf 100644 --- a/src/Controller/IndexController.php +++ b/src/Controller/IndexController.php @@ -18,7 +18,7 @@ class IndexController extends AbstractController $sm = $repo->getSchemaManager(); $entity8 = $repo->findOneByName('entity 8'); - $sub = $subRepo->findOneById(1); + $sub = $subRepo->findOne(); dump($sub); dump($sub->getService()); diff --git a/src/DataFixtures/MerFixtures.php b/src/DataFixtures/MerFixtures.php index f1ea7a0..a85e58d 100644 --- a/src/DataFixtures/MerFixtures.php +++ b/src/DataFixtures/MerFixtures.php @@ -34,7 +34,7 @@ class MerFixtures extends Fixture $manager->persist($tmp); $sub = new MerParaSub(); - $sub->setService($tmp)->setRedCode('redcode1'); + $sub->setService($tmp)->setRedCode('redcode1')->setSubDate(new \DateTimeImmutable('now')); $manager->persist($sub); $manager->flush(); diff --git a/src/Entity/MerParaSub.php b/src/Entity/MerParaSub.php index 416ae56..e80a4d9 100644 --- a/src/Entity/MerParaSub.php +++ b/src/Entity/MerParaSub.php @@ -20,6 +20,12 @@ class MerParaSub #[ORM\JoinColumn(nullable: false)] private MerService $service; + #[ORM\Column(type: 'datetime_immutable')] + private $subDate; + + #[ORM\Column(type: 'datetime_immutable', nullable: true)] + private $unsubDate; + public function getId(): ?int { return $this->id; @@ -48,4 +54,28 @@ class MerParaSub return $this; } + + public function getSubDate(): ?\DateTimeImmutable + { + return $this->subDate; + } + + public function setSubDate(\DateTimeImmutable $subDate): self + { + $this->subDate = $subDate; + + return $this; + } + + public function getUnsubDate(): ?\DateTimeImmutable + { + return $this->unsubDate; + } + + public function setUnsubDate(?\DateTimeImmutable $unsubDate): self + { + $this->unsubDate = $unsubDate; + + return $this; + } } diff --git a/src/Repository/MerParaSubRepository.php b/src/Repository/MerParaSubRepository.php index 895f628..71c0549 100644 --- a/src/Repository/MerParaSubRepository.php +++ b/src/Repository/MerParaSubRepository.php @@ -61,6 +61,16 @@ class MerParaSubRepository extends ServiceEntityRepository ->setParameter('val', $id) ->getQuery() ->getOneOrNullResult() - ; + ; + } + + public function findOne(): ?MerParaSub + { + return $this->createQueryBuilder('m') + ->orderBy('m.id', 'DESC') + ->setMaxResults(1) + ->getQuery() + ->getOneOrNullResult() + ; } }