Ported Blog\Tag entity and repo.

This commit is contained in:
Alexey Skobkin 2023-03-19 17:23:30 +03:00
parent fa9a84f2a7
commit c5a63a4259
No known key found for this signature in database
GPG Key ID: 5D5CEF6F221278E7
4 changed files with 80 additions and 69 deletions

View File

@ -1,44 +0,0 @@
<?php
namespace src\PointToolsBundle\Entity\Blogs;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(name="tags", schema="posts")
* @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Repository\Blogs\TagRepository", readOnly=true)
*/
class Tag
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="text", type="text", unique=true)
*/
private $text;
public function __construct(string $text)
{
$this->text = $text;
}
public function getId(): int
{
return $this->id;
}
public function getText(): string
{
return $this->text;
}
}

View File

@ -1,25 +0,0 @@
<?php
namespace src\PointToolsBundle\Repository\Blogs;
use Doctrine\ORM\EntityRepository;
use src\PointToolsBundle\Entity\Blogs\Tag;
use function Skobkin\Bundle\PointToolsBundle\Repository\Blogs\mb_strtolower;
class TagRepository extends EntityRepository
{
public function add(Tag $entity): void
{
$this->getEntityManager()->persist($entity);
}
public function findOneByLowerText(string $text): ?Tag
{
$qb = $this->createQueryBuilder('t');
return $qb
->where('LOWER(t.text) = :text')
->setParameter('text', mb_strtolower($text))
->getQuery()->getOneOrNullResult()
;
}
}

37
src/Entity/Blog/Tag.php Normal file
View File

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace App\Entity\Blog;
use App\Entity\Blog\Post;
use App\Repository\Blog\TagRepository;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: TagRepository::class)]
#[ORM\Table(name: 'tags', schema: 'posts')]
class Tag
{
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
#[ORM\Column(name: 'id', type: 'integer')]
private ?int $id;
#[ORM\Column(name: 'text', type: 'text', unique: true)]
private string $text;
public function __construct(string $text)
{
$this->text = $text;
}
public function getId(): ?int
{
return $this->id;
}
public function getText(): string
{
return $this->text;
}
}

View File

@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace App\Repository\Blog;
use App\Entity\Blog\Tag;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Tag>
*
* @method Tag|null find($id, $lockMode = null, $lockVersion = null)
* @method Tag|null findOneBy(array $criteria, array $orderBy = null)
* @method Tag[] findAll()
* @method Tag[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class TagRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Tag::class);
}
public function save(Tag $entity, bool $flush = false): void
{
$this->getEntityManager()->persist($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
public function findOneByLowerText(string $text): ?Tag
{
$qb = $this->createQueryBuilder('t');
return $qb
->where('LOWER(t.text) = :text')
->setParameter('text', \mb_strtolower($text))
->getQuery()->getOneOrNullResult()
;
}
}