From a445824a81867be9119540017f6d9721fc5c4a13 Mon Sep 17 00:00:00 2001 From: Petr Parolek Date: Tue, 6 Feb 2024 22:59:44 +0100 Subject: [PATCH] Doctrine annotations -> attributes --- .../Database/Advanced/Entity/Article.php | 72 +++++++------------ .../Advanced/Entity/ArticleCategory.php | 67 +++++++---------- app/Model/Database/Basic/Entity/Book.php | 47 +++++------- app/Model/Database/Basic/Entity/Category.php | 21 +++--- app/Model/Database/Basic/Entity/Tag.php | 21 +++--- config/services.neon | 4 +- ruleset.xml | 1 + 7 files changed, 88 insertions(+), 145 deletions(-) diff --git a/app/Model/Database/Advanced/Entity/Article.php b/app/Model/Database/Advanced/Entity/Article.php index 0fa0b72..2f78a8d 100644 --- a/app/Model/Database/Advanced/Entity/Article.php +++ b/app/Model/Database/Advanced/Entity/Article.php @@ -2,81 +2,63 @@ namespace App\Model\Database\Advanced\Entity; +use App\Model\Database\Advanced\Repository\ArticleRepository; use App\Model\Database\Entity\Entity; use DateTime; use Doctrine\ORM\Mapping as ORM; use Gedmo\Mapping\Annotation as Gedmo; use Gedmo\Translatable\Translatable; -/** - * @ORM\Table - * @ORM\Entity(repositoryClass="App\Model\Database\Advanced\Repository\ArticleRepository") - * @Gedmo\Loggable - * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false, hardDelete=true) - */ +#[ORM\Table] +#[ORM\Entity(repositoryClass: ArticleRepository::class)] +#[Gedmo\Loggable] +#[Gedmo\SoftDeleteable(fieldName: 'deletedAt', timeAware: false, hardDelete: true)] class Article extends Entity implements Translatable { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue()] + #[ORM\Column(name: 'id', type: 'integer')] private int $id; - /** - * @ORM\ManyToOne(targetEntity="ArticleCategory", inversedBy="articles") - * @ORM\JoinColumn(name="category_id", referencedColumnName="id", onDelete="CASCADE") - */ + #[ORM\ManyToOne(targetEntity: ArticleCategory::class, inversedBy: 'articles')] + #[ORM\JoinColumn(name: 'category_id', referencedColumnName: 'id', onDelete: 'CASCADE')] private ?ArticleCategory $category; - /** - * @Gedmo\Translatable - * @Gedmo\Versioned - * @ORM\Column(name="title", type="string", length=128) - */ + #[Gedmo\Translatable] + #[Gedmo\Versioned] + #[ORM\Column(name: 'title', type: 'string', length: 128)] private string $title; - /** - * @Gedmo\Slug(fields={"title"}) - * @ORM\Column(length=128, unique=true) - */ + #[Gedmo\Slug(fields: ['title'])] + #[ORM\Column(length: 128, unique: true)] private string $slug; - /** - * @Gedmo\Translatable - * @Gedmo\Versioned - * @ORM\Column(name="content", type="text") - */ + #[Gedmo\Translatable] + #[Gedmo\Versioned] + #[ORM\Column(name: 'content', type: 'text')] private string $content; /** * Used locale to override Translation listener`s locale * this is not a mapped field of entity metadata, just a simple property - * - * @Gedmo\Locale - */ + */ + #[Gedmo\Locale] private ?string $locale = null; - /** - * @Gedmo\Timestampable(on="create") - * @ORM\Column(type="datetime") - */ + #[Gedmo\Timestampable(on: 'create')] + #[ORM\Column(type: 'datetime')] private DateTime $created; - /** - * @Gedmo\Timestampable(on="update") - * @ORM\Column(type="datetime") - */ + #[Gedmo\Timestampable(on: 'update')] + #[ORM\Column(type: 'datetime')] private DateTime $updated; - /** - * @ORM\Column(name="content_changed", type="datetime", nullable=true) - * @Gedmo\Timestampable(on="change", field={"title", "content"}) - */ + #[ORM\Column(name: 'content_changed', type: 'datetime', nullable: true)] + #[Gedmo\Timestampable(on: 'change', field: ['title', 'content'])] private ?DateTime $contentChanged; - /** @ORM\Column(name="deletedAt", type="datetime", nullable=true) */ + #[ORM\Column(name: 'deletedAt', type: 'datetime', nullable: true)] private ?DateTime $deletedAt; public function getId(): int diff --git a/app/Model/Database/Advanced/Entity/ArticleCategory.php b/app/Model/Database/Advanced/Entity/ArticleCategory.php index 37d1bbb..2e9a4e1 100644 --- a/app/Model/Database/Advanced/Entity/ArticleCategory.php +++ b/app/Model/Database/Advanced/Entity/ArticleCategory.php @@ -2,72 +2,55 @@ namespace App\Model\Database\Advanced\Entity; +use App\Model\Database\Advanced\Repository\ArticleCategoryRepository; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; use Gedmo\Mapping\Annotation as Gedmo; -/** - * @Gedmo\Tree(type="nested") - * @ORM\Table - * @ORM\Entity(repositoryClass="App\Model\Database\Advanced\Repository\ArticleCategoryRepository") - */ +#[Gedmo\Tree(type: 'nested')] +#[ORM\Table] +#[ORM\Entity(repositoryClass: ArticleCategoryRepository::class)] class ArticleCategory { - /** - * @ORM\Column(name="id", type="integer") - * @ORM\Id - * @ORM\GeneratedValue - */ + #[ORM\Column(name: 'id', type: 'integer')] + #[ORM\Id] + #[ORM\GeneratedValue()] private int $id; - /** @ORM\Column(name="title", type="string", length=64) */ + #[ORM\Column(name: 'title', type: 'string', length: 64)] private string $title; - /** - * @Gedmo\TreeLeft - * @ORM\Column(name="lft", type="integer") - */ + #[Gedmo\TreeLeft] + #[ORM\Column(name: 'lft', type: 'integer')] private int $lft; - /** - * @Gedmo\TreeLevel - * @ORM\Column(name="lvl", type="integer") - */ + #[Gedmo\TreeLevel] + #[ORM\Column(name: 'lvl', type: 'integer')] private int $lvl; - /** - * @Gedmo\TreeRight - * @ORM\Column(name="rgt", type="integer") - */ + #[Gedmo\TreeRight] + #[ORM\Column(name: 'rgt', type: 'integer')] private int $rgt; - /** - * @Gedmo\TreeRoot - * @ORM\ManyToOne(targetEntity="ArticleCategory") - * @ORM\JoinColumn(name="tree_root", referencedColumnName="id", onDelete="CASCADE") - */ + #[Gedmo\TreeRoot] + #[ORM\ManyToOne(targetEntity: self::class)] + #[ORM\JoinColumn(name: 'tree_root', referencedColumnName: 'id', onDelete: 'CASCADE')] private ?self $root = null; - /** - * @Gedmo\TreeParent - * @ORM\ManyToOne(targetEntity="ArticleCategory", inversedBy="children") - * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE") - */ + #[Gedmo\TreeParent] + #[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')] + #[ORM\JoinColumn(name: 'parent_id', referencedColumnName: 'id', onDelete: 'CASCADE')] private ?self $parent = null; - /** - * @var Collection&iterable - * @ORM\OneToMany(targetEntity="ArticleCategory", mappedBy="parent") - * @ORM\OrderBy({"lft" = "ASC"}) - */ + /** @var Collection&iterable */ + #[ORM\OneToMany(targetEntity: self::class, mappedBy: 'parent')] + #[ORM\OrderBy(['lft' => 'ASC'])] private Collection $children; - /** - * @var Collection&iterable
- * @ORM\OneToMany(targetEntity="Article", mappedBy="category") - */ + /** @var Collection&iterable
*/ + #[ORM\OneToMany(targetEntity: Article::class, mappedBy: 'category')] private Collection $articles; public function __construct() diff --git a/app/Model/Database/Basic/Entity/Book.php b/app/Model/Database/Basic/Entity/Book.php index 58dcf64..887f50e 100644 --- a/app/Model/Database/Basic/Entity/Book.php +++ b/app/Model/Database/Basic/Entity/Book.php @@ -2,48 +2,41 @@ namespace App\Model\Database\Basic\Entity; +use App\Model\Database\Basic\Repository\BookRepository; use App\Model\Database\Entity\Entity; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Event\PreRemoveEventArgs; use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity(repositoryClass="App\Model\Database\Basic\Repository\BookRepository") - * @ORM\HasLifecycleCallbacks - */ +#[ORM\Entity(repositoryClass: BookRepository::class)] +#[ORM\HasLifecycleCallbacks] class Book extends Entity { - /** - * @ORM\Column(name="id", type="integer") - * @ORM\Id - * @ORM\GeneratedValue - */ + #[ORM\Column(name: 'id', type: 'integer')] + #[ORM\Id] + #[ORM\GeneratedValue()] private int $id; - /** @ORM\Column(type="string") */ + #[ORM\Column(type: 'string')] private string $title; - /** @ORM\Column(type="boolean") */ + #[ORM\Column(type: 'boolean')] private bool $alreadyRead = false; - /** @ORM\Column(type="string") */ + #[ORM\Column(type: 'string')] private string $createdAt; - /** @ORM\Column(type="string", nullable=true) */ + #[ORM\Column(type: 'string', nullable: true)] private ?string $updatedAt = null; - /** - * @ORM\ManyToOne(targetEntity="Category", inversedBy="books") - * @ORM\JoinColumn(nullable=FALSE) - */ + #[ORM\ManyToOne(targetEntity: Category::class, inversedBy: 'books')] + #[ORM\JoinColumn(nullable: false)] private Category $category; - /** - * @var Tag[]|Collection - * @ORM\ManyToMany(targetEntity="Tag", mappedBy="books") - */ + /** @var Tag[]|Collection */ + #[ORM\ManyToMany(targetEntity: Tag::class, mappedBy:'books')] private Collection $tags; public function __construct() @@ -104,25 +97,19 @@ public function getUpdatedAt(): ?string return $this->updatedAt; } - /** - * @ORM\PrePersist - */ + #[ORM\PrePersist] public function onPrePersist(): void { $this->createdAt = $this->getCurrentDate(); } - /** - * @ORM\PreUpdate() - */ + #[ORM\PreUpdate] public function onPreUpdate(): void { $this->updatedAt = $this->getCurrentDate(); } - /** - * @ORM\PreRemove() - */ + #[ORM\PreRemove] public function onPreRemove(PreRemoveEventArgs $args): void { /* diff --git a/app/Model/Database/Basic/Entity/Category.php b/app/Model/Database/Basic/Entity/Category.php index c75c5e7..b2d0816 100644 --- a/app/Model/Database/Basic/Entity/Category.php +++ b/app/Model/Database/Basic/Entity/Category.php @@ -2,31 +2,26 @@ namespace App\Model\Database\Basic\Entity; +use App\Model\Database\Basic\Repository\CategoryRepository; use App\Model\Database\Entity\Entity; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity(repositoryClass="App\Model\Database\Basic\Repository\CategoryRepository") - */ +#[ORM\Entity(repositoryClass: CategoryRepository::class)] class Category extends Entity { - /** - * @ORM\Column(name="id", type="integer") - * @ORM\Id - * @ORM\GeneratedValue - */ + #[ORM\Column(name: 'id', type: 'integer')] + #[ORM\Id] + #[ORM\GeneratedValue()] private int $id; - /** @ORM\Column(type="string") */ + #[ORM\Column(type: 'string')] private string $title; - /** - * @var Book[]|Collection - * @ORM\OneToMany(targetEntity="Book", mappedBy="category") - */ + /** @var Book[]|Collection */ + #[ORM\OneToMany(targetEntity: Book::class, mappedBy: 'category')] private Collection $books; /** diff --git a/app/Model/Database/Basic/Entity/Tag.php b/app/Model/Database/Basic/Entity/Tag.php index f74026c..51ff717 100644 --- a/app/Model/Database/Basic/Entity/Tag.php +++ b/app/Model/Database/Basic/Entity/Tag.php @@ -2,31 +2,26 @@ namespace App\Model\Database\Basic\Entity; +use App\Model\Database\Basic\Repository\TagRepository; use App\Model\Database\Entity\Entity; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity(repositoryClass="App\Model\Database\Basic\Repository\TagRepository") - */ +#[ORM\Entity(repositoryClass: TagRepository::class)] class Tag extends Entity { - /** - * @ORM\Column(name="id", type="integer") - * @ORM\Id - * @ORM\GeneratedValue - */ + #[ORM\Column(name: 'id', type: 'integer')] + #[ORM\Id] + #[ORM\GeneratedValue()] private int $id; - /** @ORM\Column(type="string") */ + #[ORM\Column(type: 'string')] private string $title; - /** - * @var Book[]|Collection - * @ORM\ManyToMany(targetEntity="Book", inversedBy="tags") - */ + /** @var Book[]|Collection */ + #[ORM\ManyToMany(targetEntity: Book::class, inversedBy: 'tags')] private Collection $books; /** diff --git a/config/services.neon b/config/services.neon index 7c3142e..14b6d2d 100644 --- a/config/services.neon +++ b/config/services.neon @@ -30,7 +30,7 @@ extensions: nettrine.orm: Nettrine\ORM\DI\OrmExtension nettrine.orm.cache: Nettrine\ORM\DI\OrmCacheExtension nettrine.orm.console: Nettrine\ORM\DI\OrmConsoleExtension(%consoleMode%) - nettrine.orm.annotations: Nettrine\ORM\DI\OrmAnnotationsExtension + nettrine.orm.attributes: Nettrine\ORM\DI\OrmAttributesExtension translation: Contributte\Translation\DI\TranslationExtension @@ -71,7 +71,7 @@ nettrine.migrations: nettrine.orm: entityManagerDecoratorClass: App\Model\Database\EntityManagerDecorator -nettrine.orm.annotations: +nettrine.orm.attributes: mapping: App\Model\Database\Basic\Entity: %appDir%/Model/Database/Basic/Entity App\Model\Database\Advanced\Entity: %appDir%/Model/Database/Advanced/Entity diff --git a/ruleset.xml b/ruleset.xml index cf0acbf..7116397 100644 --- a/ruleset.xml +++ b/ruleset.xml @@ -7,6 +7,7 @@ +