瀏覽代碼

Bon courage !

ideas
Simon Vieille 3 年之前
父節點
當前提交
1f50698e17
共有 4 個文件被更改,包括 153 次插入0 次删除
  1. +93
    -0
      Repository/AbstractRepositoryQuery.php
  2. +15
    -0
      Repository/Reminder/ReminderRepository2.php
  3. +14
    -0
      Repository/Reminder/ReminderRepositoryQuery.php
  4. +31
    -0
      Repository/Reminder/ReminderStore.php

+ 93
- 0
Repository/AbstractRepositoryQuery.php 查看文件

@@ -0,0 +1,93 @@
<?php

namespace Lc\SovBundle\Repository;

use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\QueryBuilder;
use Knp\Component\Pager\PaginatorInterface;

abstract class AbstractRepositoryQuery
{
protected ServiceEntityRepository $repository;
protected QueryBuilder $query;
protected PaginatorInterface $paginator;
protected string $id;

public function __construct(ServiceEntityRepository $repository, string $id, PaginatorInterface $paginator = null)
{
$this->repository = $repository;
$this->query = $repository->createQueryBuilder($id);
$this->paginator = $paginator;
$this->id = $id;
}

public function __call(string $name, $params): self
{
foreach ($params as $key => $value) {
$this->populateDqlId($params[$key]);
}

call_user_func_array([$this->query, $name], $params);

return $this;
}

public function create()
{
$class = get_called_class();

return new $class($this->repository, $this->paginator);
}

public function call(callable $fn): self
{
$fn($this->query, $this);

return $this;
}

public function findOne()
{
return $this->query->getQuery()
->setMaxResults(1)
->getOneOrNullResult()
;
}

public function find()
{
return $this->query->getQuery()->getResult();
}

public function paginate(int $page = 1, int $limit = 20)
{
return $this->paginator->paginate($this->query->getQuery(), $page, $limit);
}

public function getRepository(): ServiceEntityRepository
{
return $this->repository;
}

protected function populateDqlId(&$data)
{
if (is_string($data)) {
$words = explode(' ', $data);

foreach ($words as $k => $v) {
if (isset($v[0]) && '.' === $v[0]) {
$words[$k] = $this->id.$v;
}
}

$data = implode(' ', $words);
} elseif (is_array($data)) {
foreach ($data as $k => $v) {
$this->populateDqlId($data[$k]);
}
}

return $data;
}
}


+ 15
- 0
Repository/Reminder/ReminderRepository2.php 查看文件

@@ -0,0 +1,15 @@
<?php

namespace Lc\SovBundle\Repository\Reminder;

use App\Entity\Reminder\Reminder;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

class ReminderRepository2 extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Reminder::class);
}
}

+ 14
- 0
Repository/Reminder/ReminderRepositoryQuery.php 查看文件

@@ -0,0 +1,14 @@
<?php

namespace Lc\SovBundle\Repository\Reminder;

use Knp\Component\Pager\PaginatorInterface;
use Lc\SovBundle\Repository\AbstractRepositoryQuery;

class ReminderRepositoryQuery extends AbstractRepositoryQuery
{
public function __construct(ReminderRepository2 $repository, PaginatorInterface $paginator)
{
parent::__construct($repository, 'r', $paginator);
}
}

+ 31
- 0
Repository/Reminder/ReminderStore.php 查看文件

@@ -0,0 +1,31 @@
<?php

namespace Lc\SovBundle\Repository\Reminder;

/**
* class ReminderStore.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class ReminderStore
{
protected ReminderRepositoryQuery $query;

public function __construct(ReminderRepositoryQuery $query)
{
$this->query = $query;
}

public function getFoo($query = null)
{
if (!$query) {
$query = $this->query->create();
}

return $query
->useCustomFilters()
->andWhere('.description = :description')
->setParameter(':description', 'ahah')
->findOne();
}
}

Loading…
取消
儲存