Transform join table between Person and AccompanyingPeriod in Doctrine entity

This commit is contained in:
Mathieu Jaumotte 2021-03-31 11:21:51 +02:00
parent bd7b4b3fdb
commit a98056d31b
5 changed files with 201 additions and 12 deletions

View File

@ -77,9 +77,9 @@ class AccompanyingPeriod
/** /**
* @var Collection * @var Collection
* @ORM\ManyToMany( *
* targetEntity="Chill\PersonBundle\Entity\Person", * @ORM\OneToMany(targetEntity=AccompanyingPeriodParticipation::class,
* mappedBy="accompanyingPeriods") * mappedBy="accompanyingPeriod")
*/ */
private $persons; private $persons;

View File

@ -0,0 +1,65 @@
<?php
/*
* Chill is a software for social workers
*
* Copyright (C) 2021, Champs Libres Cooperative SCRLFS,
* <http://www.champs-libres.coop>, <info@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\PersonBundle\Entity;
use Chill\PersonBundle\Repository\AccompanyingPeriodParticipationRepository;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\Person;
use Doctrine\ORM\Mapping as ORM;
/**
* AccompanyingPeriodParticipation Class
*
* @package Chill\PersonBundle\Entity
* @ORM\Entity(repositoryClass=AccompanyingPeriodParticipationRepository::class)
* @ORM\Table(name="chill_person_accompanying_period_participation")
*/
class AccompanyingPeriodParticipation
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=Person::class,
* inversedBy="accompanyingPeriods")
* @ORM\JoinColumn(name="person_id", referencedColumnName="id", nullable=false)
*/
private $person;
/**
* @ORM\ManyToOne(targetEntity=AccompanyingPeriod::class,
* inversedBy="persons")
* @ORM\JoinColumn(name="accompanyingperiod_id", referencedColumnName="id", nullable=false)
*/
private $accompanyingPeriod;
public function getId(): ?int
{
return $this->id;
}
}

View File

@ -42,7 +42,6 @@ use Symfony\Component\Validator\Context\ExecutionContextInterface;
* name="person_names", * name="person_names",
* columns={"firstName", "lastName"} * columns={"firstName", "lastName"}
* )}) * )})
* sf4 check index name
* @ORM\HasLifecycleCallbacks() * @ORM\HasLifecycleCallbacks()
*/ */
class Person implements HasCenterInterface class Person implements HasCenterInterface
@ -204,17 +203,13 @@ class Person implements HasCenterInterface
/** /**
* The person's accompanying periods (when the person was accompanied by the center) * The person's accompanying periods (when the person was accompanied by the center)
* @var ArrayCollection * @var Collection
* *
* @ORM\ManyToMany( * @ORM\OneToMany(targetEntity=AccompanyingPeriodParticipation::class,
* targetEntity="Chill\PersonBundle\Entity\AccompanyingPeriod", * mappedBy="person",
* inversedBy="persons",
* cascade={"persist", "remove", "merge", "detach"}) * cascade={"persist", "remove", "merge", "detach"})
* @ORM\JoinTable(
* name="persons_accompanying_periods"
* )
*/ */
private $accompanyingPeriods; //TO-CHANGE in accompanyingHistory private $accompanyingPeriods;
/** /**
* A remark over the person * A remark over the person

View File

@ -0,0 +1,70 @@
<?php
/*
* Chill is a software for social workers
*
* Copyright (C) 2021, Champs Libres Cooperative SCRLFS,
* <http://www.champs-libres.coop>, <info@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\PersonBundle\Repository;
use Chill\PersonBundle\Entity\AccompanyingPeriodParticipation;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method AccompanyingPeriodParticipation|null find($id, $lockMode = null, $lockVersion = null)
* @method AccompanyingPeriodParticipation|null findOneBy(array $criteria, array $orderBy = null)
* @method AccompanyingPeriodParticipation[] findAll()
* @method AccompanyingPeriodParticipation[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class AccompanyingPeriodParticipationRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, AccompanyingPeriodParticipation::class);
}
// /**
// * @return AccompanyingPeriodParticipation[] Returns an array of AccompanyingPeriodParticipation objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('a')
->andWhere('a.exampleField = :val')
->setParameter('val', $value)
->orderBy('a.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?AccompanyingPeriodParticipation
{
return $this->createQueryBuilder('a')
->andWhere('a.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}

View File

@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace Chill\Migrations\Person;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Transform join table between Person and AccompanyingPeriod in Doctrine entity
*/
final class Version20210331084527 extends AbstractMigration
{
public function getDescription() : string
{
return 'Transform join table between Person and AccompanyingPeriod in Doctrine entity';
}
public function up(Schema $schema) : void
{
// 1
$this->addSql('ALTER TABLE persons_accompanying_periods DROP CONSTRAINT persons_accompanying_periods_pkey');
$this->addSql('ALTER TABLE persons_accompanying_periods RENAME TO chill_person_accompanying_period_participation');
// 2
// NOT NECCESSARY $this->addSql('CREATE SEQUENCE chill_person_accompanying_period_participation_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
$this->addSql('ALTER TABLE chill_person_accompanying_period_participation ADD COLUMN id SERIAL NOT NULL PRIMARY KEY');
// TO CHECK !! automatically complete missing values allowing not null clause
// but add strange "id serial (10) default nextval('chill_person_accompanying_period_participation_id_seq':: regclass )" in column definition
// 3
$this->addSql('ALTER TABLE chill_person_accompanying_period_participation DROP CONSTRAINT fk_49a3871f217bbb47');
$this->addSql('ALTER TABLE chill_person_accompanying_period_participation DROP CONSTRAINT fk_49a3871f550b0c53');
$this->addSql('ALTER TABLE chill_person_accompanying_period_participation ADD CONSTRAINT FK_A59DF89F217BBB47 FOREIGN KEY (person_id) REFERENCES chill_person_person (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE chill_person_accompanying_period_participation ADD CONSTRAINT FK_A59DF89F550B0C53 FOREIGN KEY (accompanyingperiod_id) REFERENCES chill_person_accompanying_period (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER INDEX idx_49a3871f217bbb47 RENAME TO IDX_A59DF89F217BBB47');
$this->addSql('ALTER INDEX idx_49a3871f550b0c53 RENAME TO IDX_A59DF89F550B0C53');
}
public function down(Schema $schema) : void
{
// 3
$this->addSql('ALTER TABLE chill_person_accompanying_period_participation DROP CONSTRAINT FK_A59DF89F217BBB47');
$this->addSql('ALTER TABLE chill_person_accompanying_period_participation DROP CONSTRAINT FK_A59DF89F550B0C53');
$this->addSql('ALTER TABLE chill_person_accompanying_period_participation ADD CONSTRAINT fk_49a3871f217bbb47 FOREIGN KEY (person_id) REFERENCES chill_person_person (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE chill_person_accompanying_period_participation ADD CONSTRAINT fk_49a3871f550b0c53 FOREIGN KEY (accompanyingperiod_id) REFERENCES chill_person_accompanying_period (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER INDEX idx_a59df89f550b0c53 RENAME TO idx_49a3871f550b0c53');
$this->addSql('ALTER INDEX idx_a59df89f217bbb47 RENAME TO idx_49a3871f217bbb47');
// 2
$this->addSql('DROP SEQUENCE chill_person_accompanying_period_participation_id_seq CASCADE');
$this->addSql('ALTER TABLE chill_person_accompanying_period_participation DROP id');
// 1
$this->addSql('ALTER TABLE chill_person_accompanying_period_participation RENAME TO persons_accompanying_periods');
$this->addSql('ALTER TABLE persons_accompanying_periods ADD CONSTRAINT persons_accompanying_periods_pkey PRIMARY KEY (person_id, accompanyingperiod_id)');
}
}