Ported Subscription entity and SubscriptionRepository.
This commit is contained in:
parent
201356938f
commit
5b0fd11f31
|
@ -1,48 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace src\PointToolsBundle\Entity;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use src\PointToolsBundle\Entity\User;
|
||||
|
||||
/**
|
||||
* @ORM\Table(name="subscriptions", schema="subscriptions")
|
||||
* @ORM\Entity(repositoryClass="Skobkin\Bundle\PointToolsBundle\Repository\SubscriptionRepository", readOnly=true)
|
||||
*/
|
||||
class Subscription
|
||||
{
|
||||
/**
|
||||
* @var User
|
||||
*
|
||||
* @ORM\Id
|
||||
* @ORM\ManyToOne(targetEntity="User", inversedBy="subscribers")
|
||||
* @ORM\JoinColumn(name="author_id")
|
||||
*/
|
||||
private $author;
|
||||
|
||||
/**
|
||||
* @var User
|
||||
*
|
||||
* @ORM\Id
|
||||
* @ORM\ManyToOne(targetEntity="User", inversedBy="subscriptions")
|
||||
* @ORM\JoinColumn(name="subscriber_id")
|
||||
*/
|
||||
private $subscriber;
|
||||
|
||||
|
||||
public function __construct(User $author, User $subscriber)
|
||||
{
|
||||
$this->author = $author;
|
||||
$this->subscriber = $subscriber;
|
||||
}
|
||||
|
||||
public function getAuthor(): User
|
||||
{
|
||||
return $this->author;
|
||||
}
|
||||
|
||||
public function getSubscriber(): User
|
||||
{
|
||||
return $this->subscriber;
|
||||
}
|
||||
}
|
|
@ -1,49 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace src\PointToolsBundle\Repository;
|
||||
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use src\PointToolsBundle\Entity\Subscription;
|
||||
use src\PointToolsBundle\Entity\User;
|
||||
|
||||
class SubscriptionRepository extends EntityRepository
|
||||
{
|
||||
public function add(Subscription $entity): void
|
||||
{
|
||||
$this->getEntityManager()->persist($entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getUserSubscribersCountById(int $id): int
|
||||
{
|
||||
$qb = $this->createQueryBuilder('s');
|
||||
return $qb
|
||||
->select('COUNT(s.subscriber)')
|
||||
->innerJoin('s.author', 'sa')
|
||||
->where('sa.id = :id')
|
||||
->setParameter('id', $id)
|
||||
->getQuery()->getSingleScalarResult()
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
* @param User[] $subscribers
|
||||
*/
|
||||
public function removeSubscribers(User $user, array $subscribers): void
|
||||
{
|
||||
$qb = $this->createQueryBuilder('s');
|
||||
$qb
|
||||
->delete()
|
||||
->where('s.author = :author')
|
||||
->andWhere('s.subscriber IN (:subscribers)')
|
||||
->setParameter('author', $user->getId())
|
||||
->setParameter('subscribers', $subscribers)
|
||||
->getQuery()->execute();
|
||||
;
|
||||
}
|
||||
}
|
39
src/Entity/Subscription.php
Normal file
39
src/Entity/Subscription.php
Normal file
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use App\Repository\SubscriptionRepository;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\Entity(repositoryClass: SubscriptionRepository::class, readOnly: true)]
|
||||
#[ORM\Table(name: 'subscriptions', schema: 'subscriptions')]
|
||||
class Subscription
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'subscribers')]
|
||||
#[ORM\JoinColumn(name: 'author_id')]
|
||||
private User $author;
|
||||
|
||||
#[ORM\Id]
|
||||
#[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'subscriptions')]
|
||||
#[ORM\JoinColumn(name: 'subscriber_id')]
|
||||
private User $subscriber;
|
||||
|
||||
public function __construct(User $author, User $subscriber)
|
||||
{
|
||||
$this->author = $author;
|
||||
$this->subscriber = $subscriber;
|
||||
}
|
||||
|
||||
public function getAuthor(): User
|
||||
{
|
||||
return $this->author;
|
||||
}
|
||||
|
||||
public function getSubscriber(): User
|
||||
{
|
||||
return $this->subscriber;
|
||||
}
|
||||
}
|
60
src/Repository/SubscriptionRepository.php
Normal file
60
src/Repository/SubscriptionRepository.php
Normal file
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Repository;
|
||||
|
||||
use App\Entity\Subscription;
|
||||
use App\Entity\User;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<Subscription>
|
||||
*
|
||||
* @method Subscription|null find($id, $lockMode = null, $lockVersion = null)
|
||||
* @method Subscription|null findOneBy(array $criteria, array $orderBy = null)
|
||||
* @method Subscription[] findAll()
|
||||
* @method Subscription[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
|
||||
*/
|
||||
class SubscriptionRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, Subscription::class);
|
||||
}
|
||||
|
||||
public function save(Subscription $entity, bool $flush = false): void
|
||||
{
|
||||
$this->getEntityManager()->persist($entity);
|
||||
|
||||
if ($flush) {
|
||||
$this->getEntityManager()->flush();
|
||||
}
|
||||
}
|
||||
|
||||
public function getUserSubscribersCountById(int $id): int
|
||||
{
|
||||
$qb = $this->createQueryBuilder('s');
|
||||
return $qb
|
||||
->select('COUNT(s.subscriber)')
|
||||
->innerJoin('s.author', 'sa')
|
||||
->where('sa.id = :id')
|
||||
->setParameter('id', $id)
|
||||
->getQuery()->getSingleScalarResult()
|
||||
;
|
||||
}
|
||||
|
||||
/** @param User[] $subscribers */
|
||||
public function removeSubscribers(User $user, array $subscribers): void
|
||||
{
|
||||
$qb = $this->createQueryBuilder('s');
|
||||
$qb
|
||||
->delete()
|
||||
->where('s.author = :author')
|
||||
->andWhere('s.subscriber IN (:subscribers)')
|
||||
->setParameter('author', $user->getId())
|
||||
->setParameter('subscribers', $subscribers)
|
||||
->getQuery()->execute();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue