diff --git a/src/CoreBundle/Entity/Promotion.php b/src/CoreBundle/Entity/Promotion.php index fad3ad595e..666201645c 100644 --- a/src/CoreBundle/Entity/Promotion.php +++ b/src/CoreBundle/Entity/Promotion.php @@ -6,6 +6,7 @@ declare(strict_types=1); namespace Chamilo\CoreBundle\Entity; +use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; use Gedmo\Timestampable\Traits\TimestampableEntity; use Symfony\Component\Validator\Constraints as Assert; @@ -47,6 +48,15 @@ class Promotion */ protected Career $career; + /** + * @var Collection|Session[] + * + * @ORM\OneToMany( + * targetEntity="Chamilo\CoreBundle\Entity\Session", mappedBy="promotion", cascade={"persist"} + * ) + */ + protected Collection $sessions; + /** * @ORM\Column(name="status", type="integer", nullable=false) */ @@ -67,12 +77,7 @@ class Promotion return $this->id; } - /** - * Set name. - * - * @return Promotion - */ - public function setName(string $name) + public function setName(string $name): self { $this->name = $name; diff --git a/src/CoreBundle/Entity/Session.php b/src/CoreBundle/Entity/Session.php index 7e99e035ee..d1d8eebc43 100644 --- a/src/CoreBundle/Entity/Session.php +++ b/src/CoreBundle/Entity/Session.php @@ -187,6 +187,15 @@ class Session */ protected User $sessionAdmin; + /** + * @Assert\NotBlank + * @Groups({"session:read", "session:write"}) + * + * @ORM\ManyToOne(targetEntity="User", inversedBy="sessionsAsGeneralCoach") + * @ORM\JoinColumn(name="id_coach", referencedColumnName="id") + */ + protected User $generalCoach; + /** * @Groups({"session:read"}) * @ORM\Column(name="visibility", type="integer") @@ -194,9 +203,10 @@ class Session protected int $visibility; /** - * @ORM\Column(name="promotion_id", type="integer", nullable=true, unique=false) + * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Promotion", inversedBy="sessions", cascade={"persist"}) + * @ORM\JoinColumn(name="promotion_id", referencedColumnName="id", onDelete="CASCADE") */ - protected ?int $promotionId = null; + protected ?Promotion $promotion; /** * @Groups({"session:read"}) @@ -242,15 +252,6 @@ class Session */ protected int $status; - /** - * @Assert\NotBlank - * @Groups({"session:read", "session:write"}) - * - * @ORM\ManyToOne(targetEntity="User", inversedBy="sessionsAsGeneralCoach") - * @ORM\JoinColumn(name="id_coach", referencedColumnName="id") - */ - protected User $generalCoach; - /** * @Groups({"session:read", "session:write"}) * @ORM\ManyToOne(targetEntity="SessionCategory", inversedBy="session") @@ -658,26 +659,16 @@ class Session return $this->visibility; } - /** - * Set promotionId. - * - * @return Session - */ - public function setPromotionId(int $promotionId) + public function getPromotion(): ?Promotion { - $this->promotionId = $promotionId; - - return $this; + return $this->promotion; } - /** - * Get promotionId. - * - * @return int - */ - public function getPromotionId() + public function setPromotion(?Promotion $promotion): self { - return $this->promotionId; + $this->promotion = $promotion; + + return $this; } /** diff --git a/src/CoreBundle/Entity/SessionRelUser.php b/src/CoreBundle/Entity/SessionRelUser.php index e16fa4e5bb..a6eab69886 100644 --- a/src/CoreBundle/Entity/SessionRelUser.php +++ b/src/CoreBundle/Entity/SessionRelUser.php @@ -48,7 +48,7 @@ class SessionRelUser protected Session $session; /** - * @ORM\ManyToOne(targetEntity="User", inversedBy="sessions", cascade={"persist"}) + * @ORM\ManyToOne(targetEntity="User", inversedBy="sessionsRelUser", cascade={"persist"}) * @ORM\JoinColumn(name="user_id", referencedColumnName="id") */ protected User $user; diff --git a/src/CoreBundle/Migrations/Schema/V200/Version20190210182615.php b/src/CoreBundle/Migrations/Schema/V200/Version20190210182615.php index 3a3582c39f..3c09fd1088 100644 --- a/src/CoreBundle/Migrations/Schema/V200/Version20190210182615.php +++ b/src/CoreBundle/Migrations/Schema/V200/Version20190210182615.php @@ -23,6 +23,12 @@ class Version20190210182615 extends AbstractMigrationChamilo $this->addSql('ALTER TABLE session CHANGE position position INT DEFAULT 0 NOT NULL'); } + $this->addSql('UPDATE session SET promotion_id = NULL WHERE promotion_id = 0'); + if (false === $table->hasForeignKey('FK_D044D5D4139DF194')) { + $this->addSql('ALTER TABLE session ADD CONSTRAINT FK_D044D5D4139DF194 FOREIGN KEY (promotion_id) REFERENCES promotion (id) ON DELETE CASCADE'); + $this->addSql('CREATE INDEX IDX_D044D5D4139DF194 ON session (promotion_id);'); + } + if (false === $table->hasColumn('status')) { $this->addSql('ALTER TABLE session ADD COLUMN status INT NOT NULL'); }