zend-blog-3-backend
38 строк · 1.0 Кб
1<?php
2
3namespace App\Repository;
4
5use App\Entity\EmailSubscriptionSettings;
6use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
7use Doctrine\Persistence\ManagerRegistry;
8
9/**
10* @method EmailSubscriptionSettings|null findOneBy(array $criteria, array $orderBy = null)
11*/
12class EmailSubscriptionSettingsRepository extends ServiceEntityRepository
13{
14/**
15* @param ManagerRegistry $registry
16*/
17public function __construct(ManagerRegistry $registry)
18{
19parent::__construct($registry, EmailSubscriptionSettings::class);
20}
21
22public function findOrCreate(string $email, int $type): EmailSubscriptionSettings
23{
24$entity = $this->findOneBy(['email' => $email, 'type' => $type]);
25if (!$entity) {
26$entity = new EmailSubscriptionSettings();
27$entity
28->setEmail($email)
29->setType($type)
30;
31
32$this->getEntityManager()->persist($entity);
33$this->getEntityManager()->flush();
34}
35
36return $entity;
37}
38}
39