diff --git a/Entity/Person.php b/Entity/Person.php index 7d19ccc3c..11bad306a 100644 --- a/Entity/Person.php +++ b/Entity/Person.php @@ -113,7 +113,12 @@ class Person implements HasCenterInterface { * @var \Doctrine\Common\Collections\Collection */ private $addresses; - + + /** + * @var string + */ + private $fullnameCanonical; + public function __construct(\DateTime $opening = null) { $this->accompanyingPeriods = new ArrayCollection(); $this->spokenLanguages = new ArrayCollection(); diff --git a/Resources/config/doctrine/Person.orm.yml b/Resources/config/doctrine/Person.orm.yml index df911068b..543757f9a 100644 --- a/Resources/config/doctrine/Person.orm.yml +++ b/Resources/config/doctrine/Person.orm.yml @@ -51,6 +51,9 @@ Chill\PersonBundle\Entity\Person: type: text nullable: true length: 40 + fullnameCanonical: + type: string + length: 255 manyToOne: countryOfBirth: targetEntity: Chill\MainBundle\Entity\Country diff --git a/Resources/migrations/Version20181023101621.php b/Resources/migrations/Version20181023101621.php new file mode 100644 index 000000000..24632fc12 --- /dev/null +++ b/Resources/migrations/Version20181023101621.php @@ -0,0 +1,79 @@ +abortIf($this->connection->getDatabasePlatform()->getName() !== 'postgresql', 'Migration can only be executed safely on \'postgresql\'.'); + + $this->addSql("ALTER TABLE chill_person_person ADD fullnameCanonical VARCHAR(255) DEFAULT '' "); + $this->addSql("UPDATE chill_person_person SET fullnameCanonical=LOWER(UNACCENT(CONCAT(firstname, ' ', lastname)))"); + $this->addSql("CREATE INDEX fullnameCanonical_trgm_idx ON chill_person_person USING GIN (fullnameCanonical gin_trgm_ops)"); + + $this->addSql(<<<'SQL' + CREATE OR REPLACE FUNCTION canonicalize_fullname_on_update() RETURNS TRIGGER AS + $BODY$ + BEGIN + IF NEW.firstname <> OLD.firstname OR NEW.lastname <> OLD.lastname + THEN + UPDATE chill_person_person + SET fullnameCanonical=LOWER(UNACCENT(CONCAT(NEW.firstname, ' ', NEW.lastname))) + WHERE id=NEW.id; + END IF; + RETURN NEW; + END; + $BODY$ LANGUAGE PLPGSQL; +SQL + ); + $this->addSql(<<addSql(<<<'SQL' + CREATE OR REPLACE FUNCTION canonicalize_fullname_on_insert() RETURNS TRIGGER AS + $BODY$ + BEGIN + UPDATE chill_person_person + SET fullnameCanonical=LOWER(UNACCENT(CONCAT(NEW.firstname, ' ', NEW.lastname))) + WHERE id=NEW.id; + RETURN NEW; + END; + $BODY$ LANGUAGE PLPGSQL; +SQL + ); + $this->addSql(<<abortIf($this->connection->getDatabasePlatform()->getName() !== 'postgresql', 'Migration can only be executed safely on \'postgresql\'.'); + + $this->addSql('DROP INDEX fullnameCanonical_trgm_idx'); + $this->addSql('ALTER TABLE chill_person_person DROP fullnameCanonical'); + $this->addSql('DROP TRIGGER canonicalize_fullname_on_update ON chill_person_person'); + $this->addSql('DROP FUNCTION canonicalize_fullname_on_update()'); + $this->addSql('DROP TRIGGER canonicalize_fullname_on_insert ON chill_person_person'); + $this->addSql('DROP FUNCTION canonicalize_fullname_on_insert()'); + } +}