mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-13 13:54:23 +00:00
add a many-to-many relation to addresses
This commit is contained in:
parent
afd52294b1
commit
afe6ace331
@ -26,6 +26,8 @@ use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
|
|||||||
use Doctrine\Common\Persistence\ObjectManager;
|
use Doctrine\Common\Persistence\ObjectManager;
|
||||||
use Chill\PersonBundle\Entity\Person;
|
use Chill\PersonBundle\Entity\Person;
|
||||||
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
|
||||||
|
use Chill\MainBundle\DataFixtures\ORM\LoadPostalCodes;
|
||||||
|
use Chill\MainBundle\Entity\Address;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load people into database
|
* Load people into database
|
||||||
@ -38,6 +40,13 @@ class LoadPeople extends AbstractFixture implements OrderedFixtureInterface, Con
|
|||||||
|
|
||||||
use \Symfony\Component\DependencyInjection\ContainerAwareTrait;
|
use \Symfony\Component\DependencyInjection\ContainerAwareTrait;
|
||||||
|
|
||||||
|
protected $faker;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->faker = \Faker\Factory::create('fr_FR');
|
||||||
|
}
|
||||||
|
|
||||||
public function prepare()
|
public function prepare()
|
||||||
{
|
{
|
||||||
//prepare days, month, years
|
//prepare days, month, years
|
||||||
@ -114,12 +123,27 @@ class LoadPeople extends AbstractFixture implements OrderedFixtureInterface, Con
|
|||||||
$firstName = $this->firstNamesFemale[array_rand($this->firstNamesFemale)];
|
$firstName = $this->firstNamesFemale[array_rand($this->firstNamesFemale)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// add an address on 80% of the created people
|
||||||
|
if (rand(0,100) < 80) {
|
||||||
|
$address = $this->getRandomAddress();
|
||||||
|
// on 30% of those person, add multiple addresses
|
||||||
|
if (rand(0,10) < 4) {
|
||||||
|
$address = array(
|
||||||
|
$address,
|
||||||
|
$this->getRandomAddress()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$address = null;
|
||||||
|
}
|
||||||
|
|
||||||
$person = array(
|
$person = array(
|
||||||
'FirstName' => $firstName,
|
'FirstName' => $firstName,
|
||||||
'LastName' => $lastName,
|
'LastName' => $lastName,
|
||||||
'Gender' => $sex,
|
'Gender' => $sex,
|
||||||
'Nationality' => (rand(0,100) > 50) ? NULL: 'BE',
|
'Nationality' => (rand(0,100) > 50) ? NULL: 'BE',
|
||||||
'center' => (rand(0,1) == 0) ? 'centerA': 'centerB',
|
'center' => (rand(0,1) == 0) ? 'centerA': 'centerB',
|
||||||
|
'Address' => $address,
|
||||||
'maritalStatus' => $this->maritalStatusRef[array_rand($this->maritalStatusRef)]
|
'maritalStatus' => $this->maritalStatusRef[array_rand($this->maritalStatusRef)]
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -142,10 +166,18 @@ class LoadPeople extends AbstractFixture implements OrderedFixtureInterface, Con
|
|||||||
'Email' => "Email d'un ami: roger@tt.com",
|
'Email' => "Email d'un ami: roger@tt.com",
|
||||||
'CountryOfBirth' => 'BE',
|
'CountryOfBirth' => 'BE',
|
||||||
'Nationality' => 'BE',
|
'Nationality' => 'BE',
|
||||||
'CFData' => array()
|
'CFData' => array(),
|
||||||
|
'Address' => null
|
||||||
), $specific);
|
), $specific);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* create a new person from array data
|
||||||
|
*
|
||||||
|
* @param array $person
|
||||||
|
* @param ObjectManager $manager
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
private function addAPerson(array $person, ObjectManager $manager)
|
private function addAPerson(array $person, ObjectManager $manager)
|
||||||
{
|
{
|
||||||
$p = new Person();
|
$p = new Person();
|
||||||
@ -164,13 +196,51 @@ class LoadPeople extends AbstractFixture implements OrderedFixtureInterface, Con
|
|||||||
$value = $this->getReference($value);
|
$value = $this->getReference($value);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
call_user_func(array($p, 'set'.$key), $value);
|
|
||||||
|
//try to add the data using the setSomething function,
|
||||||
|
// if not possible, fallback to addSomething function
|
||||||
|
if (method_exists($p, 'set'.$key)) {
|
||||||
|
call_user_func(array($p, 'set'.$key), $value);
|
||||||
|
} elseif (method_exists($p, 'add'.$key)) {
|
||||||
|
// if we have a "addSomething", we may have multiple items to add
|
||||||
|
// so, we set the value in an array if it is not an array, and
|
||||||
|
// will call the function addSomething multiple times
|
||||||
|
if (!is_array($value)) {
|
||||||
|
$value = array($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach($value as $v) {
|
||||||
|
if ($v !== NULL) {
|
||||||
|
call_user_func(array($p, 'add'.$key), $v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$manager->persist($p);
|
$manager->persist($p);
|
||||||
echo "add person'".$p->__toString()."'\n";
|
echo "add person'".$p->__toString()."'\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creata a random address
|
||||||
|
*
|
||||||
|
* @return Address
|
||||||
|
*/
|
||||||
|
private function getRandomAddress()
|
||||||
|
{
|
||||||
|
return (new Address())
|
||||||
|
->setStreetAddress1($this->faker->streetAddress)
|
||||||
|
->setStreetAddress2(
|
||||||
|
rand(0,9) > 5 ? $this->faker->streetAddress : ''
|
||||||
|
)
|
||||||
|
->setPostcode($this->getReference(
|
||||||
|
LoadPostalCodes::$refs[array_rand(LoadPostalCodes::$refs)]
|
||||||
|
))
|
||||||
|
->setValidFrom($this->faker->dateTimeBetween('-5 years'))
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
private function getCountry($countryCode)
|
private function getCountry($countryCode)
|
||||||
{
|
{
|
||||||
if ($countryCode === NULL) {
|
if ($countryCode === NULL) {
|
||||||
|
@ -71,6 +71,7 @@ class Configuration implements ConfigurationInterface
|
|||||||
->append($this->addFieldNode('country_of_birth'))
|
->append($this->addFieldNode('country_of_birth'))
|
||||||
->append($this->addFieldNode('marital_status'))
|
->append($this->addFieldNode('marital_status'))
|
||||||
->append($this->addFieldNode('spoken_languages'))
|
->append($this->addFieldNode('spoken_languages'))
|
||||||
|
->append($this->addFieldNode('address'))
|
||||||
->end() //children for 'person_fields', parent = array 'person_fields'
|
->end() //children for 'person_fields', parent = array 'person_fields'
|
||||||
->end() // person_fields, parent = children of root
|
->end() // person_fields, parent = children of root
|
||||||
->end() // children of 'root', parent = root
|
->end() // children of 'root', parent = root
|
||||||
|
@ -27,6 +27,8 @@ use Chill\MainBundle\Entity\Country;
|
|||||||
use Chill\PersonBundle\Entity\MaritalStatus;
|
use Chill\PersonBundle\Entity\MaritalStatus;
|
||||||
use Doctrine\Common\Collections\ArrayCollection;
|
use Doctrine\Common\Collections\ArrayCollection;
|
||||||
use Chill\MainBundle\Entity\HasCenterInterface;
|
use Chill\MainBundle\Entity\HasCenterInterface;
|
||||||
|
use Chill\MainBundle\Entity\Address;
|
||||||
|
use Doctrine\Common\Collections\Criteria;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Person
|
* Person
|
||||||
@ -100,9 +102,16 @@ class Person implements HasCenterInterface {
|
|||||||
/** @var array Array where customfield's data are stored */
|
/** @var array Array where customfield's data are stored */
|
||||||
private $cFData;
|
private $cFData;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @var \Doctrine\Common\Collections\Collection
|
||||||
|
*/
|
||||||
|
private $addresses;
|
||||||
|
|
||||||
public function __construct(\DateTime $opening = null) {
|
public function __construct(\DateTime $opening = null) {
|
||||||
$this->accompanyingPeriods = new ArrayCollection();
|
$this->accompanyingPeriods = new ArrayCollection();
|
||||||
$this->spokenLanguages = new ArrayCollection();
|
$this->spokenLanguages = new ArrayCollection();
|
||||||
|
$this->addresses = new ArrayCollection();
|
||||||
|
|
||||||
if ($opening === null) {
|
if ($opening === null) {
|
||||||
$opening = new \DateTime();
|
$opening = new \DateTime();
|
||||||
@ -597,6 +606,50 @@ class Person implements HasCenterInterface {
|
|||||||
return $this->spokenLanguages;
|
return $this->spokenLanguages;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function addAddress(Address $address)
|
||||||
|
{
|
||||||
|
$this->addresses[] = $address;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function removeAddress(Address $address)
|
||||||
|
{
|
||||||
|
$this->addresses->removeElement($address);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return \Chill\MainBundle\Entity\Address[]@return Address[]
|
||||||
|
*/
|
||||||
|
public function getAddresses()
|
||||||
|
{
|
||||||
|
return $this->addresses;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getLastAddress(\DateTime $date = null)
|
||||||
|
{
|
||||||
|
if ($date === null) {
|
||||||
|
$date = new \DateTime('now');
|
||||||
|
}
|
||||||
|
|
||||||
|
$lastAddress = null;
|
||||||
|
|
||||||
|
foreach ($this->getAddresses() as $address) {
|
||||||
|
if ($address->getValidFrom() < $date) {
|
||||||
|
if ($lastAddress === NULL) {
|
||||||
|
$lastAddress = $address;
|
||||||
|
} else {
|
||||||
|
if ($lastAddress->getValidFrom() < $address->getValidFrom()) {
|
||||||
|
$lastAddress = $address;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $lastAddress;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validation callback that checks if the accompanying periods are valid
|
* Validation callback that checks if the accompanying periods are valid
|
||||||
*
|
*
|
||||||
|
@ -72,4 +72,9 @@ Chill\PersonBundle\Entity\Person:
|
|||||||
inverseJoinColumns:
|
inverseJoinColumns:
|
||||||
language_id:
|
language_id:
|
||||||
referencedColumnName: id
|
referencedColumnName: id
|
||||||
|
addresses:
|
||||||
|
targetEntity: Chill\MainBundle\Entity\Address
|
||||||
|
joinTable:
|
||||||
|
name: chill_person_persons_to_addresses
|
||||||
|
cascade: [persist, remove, merge, detach]
|
||||||
lifecycleCallbacks: { }
|
lifecycleCallbacks: { }
|
||||||
|
49
Resources/migrations/Version20160310161006.php
Normal file
49
Resources/migrations/Version20160310161006.php
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Application\Migrations;
|
||||||
|
|
||||||
|
use Doctrine\DBAL\Migrations\AbstractMigration;
|
||||||
|
use Doctrine\DBAL\Schema\Schema;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a many-to-many relationship between person and addresses
|
||||||
|
*/
|
||||||
|
class Version20160310161006 extends AbstractMigration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param Schema $schema
|
||||||
|
*/
|
||||||
|
public function up(Schema $schema)
|
||||||
|
{
|
||||||
|
// this up() migration is auto-generated, please modify it to your needs
|
||||||
|
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');
|
||||||
|
|
||||||
|
$this->addSql('CREATE TABLE chill_person_persons_to_addresses ('
|
||||||
|
. 'person_id INT NOT NULL, '
|
||||||
|
. 'address_id INT NOT NULL, '
|
||||||
|
. 'PRIMARY KEY(person_id, address_id))');
|
||||||
|
$this->addSql('CREATE INDEX IDX_4655A196217BBB47 '
|
||||||
|
. 'ON chill_person_persons_to_addresses (person_id)');
|
||||||
|
$this->addSql('CREATE INDEX IDX_4655A196F5B7AF75 '
|
||||||
|
. 'ON chill_person_persons_to_addresses (address_id)');
|
||||||
|
$this->addSql('ALTER TABLE chill_person_persons_to_addresses '
|
||||||
|
. 'ADD CONSTRAINT FK_4655A196217BBB47 '
|
||||||
|
. 'FOREIGN KEY (person_id) '
|
||||||
|
. 'REFERENCES Person (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
|
||||||
|
$this->addSql('ALTER TABLE chill_person_persons_to_addresses '
|
||||||
|
. 'ADD CONSTRAINT FK_4655A196F5B7AF75 '
|
||||||
|
. 'FOREIGN KEY (address_id) '
|
||||||
|
. 'REFERENCES chill_main_address (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Schema $schema
|
||||||
|
*/
|
||||||
|
public function down(Schema $schema)
|
||||||
|
{
|
||||||
|
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');
|
||||||
|
|
||||||
|
$this->addSql('DROP TABLE chill_person_persons_to_addresses');
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -70,6 +70,8 @@ Reset: 'Remise à zéro'
|
|||||||
'Person details': 'Détails de la personne'
|
'Person details': 'Détails de la personne'
|
||||||
'Update details for %name%': 'Modifier détails de %name%'
|
'Update details for %name%': 'Modifier détails de %name%'
|
||||||
Accompanying period list: Périodes d'accompagnement
|
Accompanying period list: Périodes d'accompagnement
|
||||||
|
Since %date%: Depuis le %date%
|
||||||
|
No address given: Pas d'adresse renseignée
|
||||||
|
|
||||||
#timeline
|
#timeline
|
||||||
'An accompanying period is opened for %person% on %date%': Une période d'accompagnement a été ouverte le %date% pour %person%
|
'An accompanying period is opened for %person% on %date%': Une période d'accompagnement a été ouverte le %date% pour %person%
|
||||||
|
@ -16,6 +16,8 @@
|
|||||||
#}
|
#}
|
||||||
{% extends "ChillPersonBundle::layout.html.twig" %}
|
{% extends "ChillPersonBundle::layout.html.twig" %}
|
||||||
|
|
||||||
|
{% import 'ChillMainBundle:Address:macro.html.twig' as address %}
|
||||||
|
|
||||||
{% set activeRouteKey = 'chill_person_view' %}
|
{% set activeRouteKey = 'chill_person_view' %}
|
||||||
|
|
||||||
{#
|
{#
|
||||||
@ -165,6 +167,19 @@ This view should receive those arguments:
|
|||||||
<figure class="person-details">
|
<figure class="person-details">
|
||||||
<h2 class="chill-blue"><i class="fa fa-envelope-o"></i> {{ 'Contact information'|trans|upper }}</h2>
|
<h2 class="chill-blue"><i class="fa fa-envelope-o"></i> {{ 'Contact information'|trans|upper }}</h2>
|
||||||
|
|
||||||
|
{%- if chill_person.fields.address == 'visible' -%}
|
||||||
|
<dl>
|
||||||
|
<dt>{{ 'Address'|trans }}</dt>
|
||||||
|
<dd>
|
||||||
|
{%- if person.lastAddress is not empty -%}
|
||||||
|
{{ address._render(person.lastAddress) }}
|
||||||
|
{%- else -%}
|
||||||
|
<span class=".chill-no-data-statement">{{ 'No address given'|trans }}</span>
|
||||||
|
{%- endif -%}
|
||||||
|
</dd>
|
||||||
|
</dl>
|
||||||
|
{%- endif -%}
|
||||||
|
|
||||||
{%- if chill_person.fields.email == 'visible' -%}
|
{%- if chill_person.fields.email == 'visible' -%}
|
||||||
<dl>
|
<dl>
|
||||||
<dt>{{ 'Email'|trans }} :</dt>
|
<dt>{{ 'Email'|trans }} :</dt>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user