WIP: Symfony 6 project remake #2
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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
37
src/Entity/Blog/Tag.php
Normal 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;
|
||||
}
|
||||
}
|
43
src/Repository/Blog/TagRepository.php
Normal file
43
src/Repository/Blog/TagRepository.php
Normal 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()
|
||||
;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue