Нет описания

Repository.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace MailPoet\Doctrine;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoetVendor\Doctrine\ORM\EntityManager;
  5. use MailPoetVendor\Doctrine\ORM\EntityRepository as DoctrineEntityRepository;
  6. use MailPoetVendor\Doctrine\ORM\Mapping\ClassMetadata;
  7. /**
  8. * @template T of object
  9. */
  10. abstract class Repository {
  11. /** @var EntityManager */
  12. protected $entityManager;
  13. /** @var ClassMetadata<object> */
  14. protected $classMetadata;
  15. /** @var DoctrineEntityRepository<T> */
  16. protected $doctrineRepository;
  17. /** @var string[] */
  18. protected $ignoreColumnsForUpdate = [
  19. 'created_at',
  20. ];
  21. public function __construct(EntityManager $entityManager) {
  22. $this->entityManager = $entityManager;
  23. $this->classMetadata = $entityManager->getClassMetadata($this->getEntityClassName());
  24. $this->doctrineRepository = new DoctrineEntityRepository($this->entityManager, $this->classMetadata);
  25. }
  26. /**
  27. * @param array $criteria
  28. * @param array|null $orderBy
  29. * @param int|null $limit
  30. * @param int|null $offset
  31. * @return T[]
  32. */
  33. public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) {
  34. return $this->doctrineRepository->findBy($criteria, $orderBy, $limit, $offset);
  35. }
  36. public function countBy(array $criteria): int {
  37. return $this->doctrineRepository->count($criteria);
  38. }
  39. /**
  40. * @param array $criteria
  41. * @param array|null $orderBy
  42. * @return T|null
  43. */
  44. public function findOneBy(array $criteria, array $orderBy = null) {
  45. return $this->doctrineRepository->findOneBy($criteria, $orderBy);
  46. }
  47. /**
  48. * @param mixed $id
  49. * @return T|null
  50. */
  51. public function findOneById($id) {
  52. return $this->doctrineRepository->find($id);
  53. }
  54. /**
  55. * @return T[]
  56. */
  57. public function findAll() {
  58. return $this->doctrineRepository->findAll();
  59. }
  60. /**
  61. * @param T $entity
  62. */
  63. public function persist($entity) {
  64. $this->entityManager->persist($entity);
  65. }
  66. public function truncate() {
  67. $tableName = $this->classMetadata->getTableName();
  68. $connection = $this->entityManager->getConnection();
  69. $connection->query('SET FOREIGN_KEY_CHECKS=0');
  70. $q = "TRUNCATE $tableName";
  71. $connection->executeUpdate($q);
  72. $connection->query('SET FOREIGN_KEY_CHECKS=1');
  73. }
  74. /**
  75. * @param T $entity
  76. */
  77. public function remove($entity) {
  78. $this->entityManager->remove($entity);
  79. }
  80. /**
  81. * @param T $entity
  82. */
  83. public function refresh($entity) {
  84. $this->entityManager->refresh($entity);
  85. }
  86. public function flush() {
  87. $this->entityManager->flush();
  88. }
  89. /**
  90. * @return class-string<T>
  91. */
  92. abstract protected function getEntityClassName();
  93. }