Browse Source

OneToMany IndividualData => Territory

master
Charly 3 years ago
parent
commit
4bb272361c
3 changed files with 77 additions and 24 deletions
  1. +41
    -0
      migrations/Version20210609090212.php
  2. +4
    -23
      src/Entity/IndividualData.php
  3. +32
    -1
      src/Entity/Territory.php

+ 41
- 0
migrations/Version20210609090212.php View File

@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20210609090212 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE individual_data ADD territory_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE individual_data ADD CONSTRAINT FK_653169D073F74AD4 FOREIGN KEY (territory_id) REFERENCES territory (id)');
$this->addSql('CREATE INDEX IDX_653169D073F74AD4 ON individual_data (territory_id)');
$this->addSql('ALTER TABLE territory DROP FOREIGN KEY FK_E9743966E2920B1');
$this->addSql('DROP INDEX IDX_E9743966E2920B1 ON territory');
$this->addSql('ALTER TABLE territory DROP individual_data_id');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE individual_data DROP FOREIGN KEY FK_653169D073F74AD4');
$this->addSql('DROP INDEX IDX_653169D073F74AD4 ON individual_data');
$this->addSql('ALTER TABLE individual_data DROP territory_id');
$this->addSql('ALTER TABLE territory ADD individual_data_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE territory ADD CONSTRAINT FK_E9743966E2920B1 FOREIGN KEY (individual_data_id) REFERENCES individual_data (id)');
$this->addSql('CREATE INDEX IDX_E9743966E2920B1 ON territory (individual_data_id)');
}
}

+ 4
- 23
src/Entity/IndividualData.php View File

@@ -70,7 +70,7 @@ class IndividualData implements StatusInterface, EntityInterface
private $projectsInspiring;

/**
* @ORM\OneToMany(targetEntity=Territory::class, mappedBy="individualData")
* @ORM\ManyToOne(targetEntity=Territory::class, inversedBy="individualData")
*/
private $territory;

@@ -80,7 +80,6 @@ class IndividualData implements StatusInterface, EntityInterface
$this->dreams = new ArrayCollection();
$this->projectsBoost = new ArrayCollection();
$this->projectsInspiring = new ArrayCollection();
$this->territory = new ArrayCollection();
}

public function __toString()
@@ -273,32 +272,14 @@ class IndividualData implements StatusInterface, EntityInterface
return $this;
}

/**
* @return Collection|Territory[]
*/
public function getTerritory(): Collection
public function getTerritory(): ?Territory
{
return $this->territory;
}

public function addTerritory(Territory $territory): self
public function setTerritory(?Territory $territory): self
{
if (!$this->territory->contains($territory)) {
$this->territory[] = $territory;
$territory->setIndividualData($this);
}

return $this;
}

public function removeTerritory(Territory $territory): self
{
if ($this->territory->removeElement($territory)) {
// set the owning side to null (unless already changed)
if ($territory->getIndividualData() === $this) {
$territory->setIndividualData(null);
}
}
$this->territory = $territory;

return $this;
}

+ 32
- 1
src/Entity/Territory.php View File

@@ -30,7 +30,7 @@ class Territory implements EntityInterface, DevAliasInterface
private $name;

/**
* @ORM\ManyToOne(targetEntity=IndividualData::class, inversedBy="territory")
* @ORM\OneToMany(targetEntity=IndividualData::class, mappedBy="territory")
*/
private $individualData;

@@ -42,6 +42,7 @@ class Territory implements EntityInterface, DevAliasInterface
public function __construct()
{
$this->collectifData = new ArrayCollection();
$this->individualData = new ArrayCollection();
}

public function __toString()
@@ -107,4 +108,34 @@ class Territory implements EntityInterface, DevAliasInterface

return $this;
}

/**
* @return Collection|Territory[]
*/
public function getTerritory(): Collection
{
return $this->territory;
}

public function addTerritory(Territory $territory): self
{
if (!$this->territory->contains($territory)) {
$this->territory[] = $territory;
$territory->setIndividualData($this);
}

return $this;
}

public function removeTerritory(Territory $territory): self
{
if ($this->territory->removeElement($territory)) {
// set the owning side to null (unless already changed)
if ($territory->getIndividualData() === $this) {
$territory->setIndividualData(null);
}
}

return $this;
}
}

Loading…
Cancel
Save