diff options
Diffstat (limited to 'src/Model/Entity/Article.php')
| -rw-r--r-- | src/Model/Entity/Article.php | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/src/Model/Entity/Article.php b/src/Model/Entity/Article.php new file mode 100644 index 0000000..588ebd6 --- /dev/null +++ b/src/Model/Entity/Article.php | |||
| @@ -0,0 +1,94 @@ | |||
| 1 | <?php | ||
| 2 | // src/Model/Entity/Article.php | ||
| 3 | // | ||
| 4 | // entité commune pour les "post" (articles simples) et les "news" (complexes avec titre, aperçu et contenu) | ||
| 5 | |||
| 6 | declare(strict_types=1); | ||
| 7 | |||
| 8 | namespace App\Entity; | ||
| 9 | |||
| 10 | use Doctrine\ORM\Mapping as ORM; | ||
| 11 | use Doctrine\Common\Collections\ArrayCollection; | ||
| 12 | use Doctrine\Common\Collections\Collection; | ||
| 13 | |||
| 14 | #[ORM\Entity] | ||
| 15 | #[ORM\Table(name: TABLE_PREFIX . "article")] | ||
| 16 | class Article | ||
| 17 | { | ||
| 18 | #[ORM\Id] | ||
| 19 | #[ORM\GeneratedValue] | ||
| 20 | #[ORM\Column(type: "integer")] | ||
| 21 | private int $id_article; | ||
| 22 | |||
| 23 | // datetime_immutable permet à la base de toujours gérer cette clé primaire correctement | ||
| 24 | #[ORM\Column(type: 'datetime', options: ['default' => 'CURRENT_TIMESTAMP'], unique: true)] | ||
| 25 | private ?\DateTime $date_time; // le type datetime de doctrine convertit en type \DateTime de PHP | ||
| 26 | |||
| 27 | #[ORM\Column(type: "text")] // "string" => varchar(255) => risque HTML non valide | ||
| 28 | private string $title; | ||
| 29 | |||
| 30 | #[ORM\Column(type: "text")] | ||
| 31 | private string $preview; // une simple textarea | ||
| 32 | |||
| 33 | #[ORM\Column(type: "text")] | ||
| 34 | private string $content; // de l'éditeur html | ||
| 35 | |||
| 36 | #[ORM\ManyToMany(targetEntity: Image::class, inversedBy: "article")] // cascade: ['remove'] = très dangereux! | ||
| 37 | #[ORM\JoinTable( | ||
| 38 | name: TABLE_PREFIX . "article_image", | ||
| 39 | joinColumns: [new ORM\JoinColumn(name: "article_id", referencedColumnName: "id_article", onDelete: "CASCADE")], // onDelete: "CASCADE": très utile | ||
| 40 | inverseJoinColumns: [new ORM\JoinColumn(name: "image_id", referencedColumnName: "id_image", onDelete: "CASCADE")] | ||
| 41 | )] | ||
| 42 | private Collection $images; | ||
| 43 | |||
| 44 | public function __construct(string $content, ?\DateTime $date_time = null, string $title = '', string $preview = '') | ||
| 45 | { | ||
| 46 | $this->date_time = $date_time; | ||
| 47 | $this->title = $title; | ||
| 48 | $this->preview = $preview; | ||
| 49 | $this->content = $content; | ||
| 50 | $this->images = new ArrayCollection(); // initialisation nécessaire | ||
| 51 | } | ||
| 52 | |||
| 53 | public function getDateTime(): \DateTime | ||
| 54 | { | ||
| 55 | return $this->date_time; | ||
| 56 | } | ||
| 57 | public function setDateTime(\DateTime $date_time): void | ||
| 58 | { | ||
| 59 | $this->date_time = $date_time; | ||
| 60 | } | ||
| 61 | public function getTimestamp(): int | ||
| 62 | { | ||
| 63 | return $this->date_time->getTimestamp(); | ||
| 64 | } | ||
| 65 | public function getTitle(): string | ||
| 66 | { | ||
| 67 | return $this->title; | ||
| 68 | } | ||
| 69 | public function setTitle(string $title): void | ||
| 70 | { | ||
| 71 | $this->title = $title; | ||
| 72 | } | ||
| 73 | public function getPreview(): string | ||
| 74 | { | ||
| 75 | return $this->preview; | ||
| 76 | } | ||
| 77 | public function setPreview(string $preview): void | ||
| 78 | { | ||
| 79 | $this->preview = $preview; | ||
| 80 | } | ||
| 81 | public function getContent(): string | ||
| 82 | { | ||
| 83 | return $this->content; | ||
| 84 | } | ||
| 85 | public function setContent(string $data): void | ||
| 86 | { | ||
| 87 | $this->content = $data; | ||
| 88 | } | ||
| 89 | |||
| 90 | public function getImages(): Collection | ||
| 91 | { | ||
| 92 | return $this->images; | ||
| 93 | } | ||
| 94 | } | ||
