migrations/Version20230302154818.php line 1

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace DoctrineMigrations;
  4. use App\Entity\Role;
  5. use Doctrine\DBAL\Exception;
  6. use Doctrine\DBAL\Schema\Schema;
  7. use Doctrine\Migrations\AbstractMigration;
  8. /**
  9.  * Auto-generated Migration: Please modify to your needs!
  10.  */
  11. final class Version20230302154818 extends AbstractMigration {
  12.     public function getDescription(): string {
  13.         return '';
  14.     }
  15.     public function up(Schema $schema): void {
  16.         $this->addRoleDiplome();
  17.         $this->addRoleEntreprise();
  18.         $this->addRoleEtablissement();
  19.     }
  20.     /**
  21.      * @throws Exception
  22.      */
  23.     private function addRoleEntreprise(): void {
  24.         $roleId $this->connection->executeQuery(
  25.             'SELECT r.id as id
  26.                 FROM role r
  27.                 WHERE r.role = ?',
  28.             [Role::ROLE_ENTREPRISE])
  29.             ->fetchOne();
  30.         if (!$roleId) {
  31.             $addRole $this->connection->prepare(
  32.                 'INSERT INTO role (role) VALUES (:roleName)'
  33.             );
  34.             $addRole->bindValue('roleName'Role::ROLE_ENTREPRISE);
  35.             $addRole->executeStatement();
  36.             $roleId $this->connection->lastInsertId();
  37.         }
  38.         // Add ROLE_DIPLOME to person_degree not having this role
  39.         $user_ids $this->connection->executeQuery(
  40.             'SELECT u.id as id
  41.                 FROM company p, user u
  42.                 WHERE p.user_id = u.id'
  43.         )->fetchAllAssociative();
  44.         foreach ($user_ids as $user_id) {
  45.             $this->addSql('INSERT INTO user_role (user_id, role_id)
  46.                     SELECT ?, ?
  47.                     WHERE NOT EXISTS (
  48.                         SELECT 1 FROM user_role ur
  49.                         WHERE ur.user_id = ? AND ur.role_id = ?
  50.                     )', [$user_id['id'], $roleId$user_id['id'], $roleId]);
  51.         }
  52.     }
  53.     /**
  54.      * @throws Exception
  55.      */
  56.     private function addRoleEtablissement(): void {
  57.         /// Add ROLE_ETABLISSEMENT if not exist and get ID
  58.         $roleId $this->connection->executeQuery(
  59.             'SELECT r.id as id
  60.                 FROM role r
  61.                 WHERE r.role = ?',
  62.             [Role::ROLE_ETABLISSEMENT])
  63.             ->fetchOne();
  64.         if (!$roleId) {
  65.             $addRole $this->connection->prepare(
  66.                 'INSERT INTO role (role) VALUES (:roleName)'
  67.             );
  68.             $addRole->bindValue('roleName'Role::ROLE_ETABLISSEMENT);
  69.             $addRole->executeStatement();
  70.             $roleId $this->connection->lastInsertId();
  71.         }
  72.         // Add ROLE_ETABLISSEMENT to person_degree not having this role
  73.         $user_ids $this->connection->executeQuery(
  74.             'SELECT u.id as id
  75.                 FROM school p, user u
  76.                 WHERE p.user_id = u.id'
  77.         )->fetchAllAssociative();
  78.         foreach ($user_ids as $user_id) {
  79.             $this->addSql('INSERT INTO user_role (user_id, role_id)
  80.                     SELECT ?, ?
  81.                     WHERE NOT EXISTS (
  82.                         SELECT 1 FROM user_role ur
  83.                         WHERE ur.user_id = ? AND ur.role_id = ?
  84.                     )', [$user_id['id'], $roleId$user_id['id'], $roleId]);
  85.         }
  86.     }
  87.     private function addRoleDiplome(): void {
  88.         /// Add ROLE_DIPLOME if not exist and get ID
  89.         $roleId $this->connection->executeQuery(
  90.             'SELECT r.id as id
  91.                 FROM role r
  92.                 WHERE r.role = ?',
  93.             [Role::ROLE_DIPLOME])
  94.             ->fetchOne();
  95.         if (!$roleId) {
  96.             $addRole $this->connection->prepare(
  97.                 'INSERT INTO role (role) VALUES (:roleName)'
  98.             );
  99.             $addRole->bindValue('roleName'Role::ROLE_DIPLOME);
  100.             $addRole->executeStatement();
  101.             $roleId $this->connection->lastInsertId();
  102.         }
  103.         // Add ROLE_DIPLOME to person_degree not having this role
  104.         $user_ids $this->connection->executeQuery(
  105.             'SELECT u.id as id
  106.                 FROM person_degree p, user u
  107.                 WHERE p.user_id = u.id'
  108.         )->fetchAllAssociative();
  109.         foreach ($user_ids as $user_id) {
  110.             $this->addSql('INSERT INTO user_role (user_id, role_id)
  111.                     SELECT ?, ?
  112.                     WHERE NOT EXISTS (
  113.                         SELECT 1 FROM user_role ur
  114.                         WHERE ur.user_id = ? AND ur.role_id = ?
  115.                     )', [$user_id['id'], $roleId$user_id['id'], $roleId]);
  116.         }
  117.     }
  118.     public function down(Schema $schema): void {
  119.     }
  120. }