Skip to content

Commit

Permalink
Doctrine annotations -> attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
petrparolek authored and f3l1x committed Feb 7, 2024
1 parent 7e0d7cf commit a445824
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 145 deletions.
72 changes: 27 additions & 45 deletions app/Model/Database/Advanced/Entity/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
67 changes: 25 additions & 42 deletions app/Model/Database/Advanced/Entity/ArticleCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArticleCategory>
* @ORM\OneToMany(targetEntity="ArticleCategory", mappedBy="parent")
* @ORM\OrderBy({"lft" = "ASC"})
*/
/** @var Collection&iterable<ArticleCategory> */
#[ORM\OneToMany(targetEntity: self::class, mappedBy: 'parent')]
#[ORM\OrderBy(['lft' => 'ASC'])]
private Collection $children;

/**
* @var Collection&iterable<Article>
* @ORM\OneToMany(targetEntity="Article", mappedBy="category")
*/
/** @var Collection&iterable<Article> */
#[ORM\OneToMany(targetEntity: Article::class, mappedBy: 'category')]
private Collection $articles;

public function __construct()
Expand Down
47 changes: 17 additions & 30 deletions app/Model/Database/Basic/Entity/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
{
/*
Expand Down
21 changes: 8 additions & 13 deletions app/Model/Database/Basic/Entity/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
21 changes: 8 additions & 13 deletions app/Model/Database/Basic/Entity/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
4 changes: 2 additions & 2 deletions config/services.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit a445824

Please sign in to comment.