* command and user actions added
* language model added
This commit is contained in:
99
src/App/Service/User/UserService.php
Normal file
99
src/App/Service/User/UserService.php
Normal file
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\User;
|
||||
|
||||
use App\Entity\User;
|
||||
use App\Hydrator\DoctrineObject;
|
||||
use App\Service\EntityServiceInterface;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Doctrine\ORM\Query;
|
||||
use Exception;
|
||||
|
||||
class UserService implements EntityServiceInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var EntityManager
|
||||
*/
|
||||
private $em;
|
||||
|
||||
/**
|
||||
* @var DoctrineObject
|
||||
*/
|
||||
private $hydrator;
|
||||
|
||||
public function __construct(EntityManager $em, DoctrineObject $hydrator)
|
||||
{
|
||||
$this->em = $em;
|
||||
$this->hydrator = $hydrator;
|
||||
}
|
||||
|
||||
public function getList()
|
||||
{
|
||||
$qb = $this->em->createQueryBuilder();
|
||||
$entities = $qb->select('u,a,c')
|
||||
->from(User::class, 'u')
|
||||
->leftJoin('u.articles', 'a')
|
||||
->leftJoin('u.comments', 'c')
|
||||
->getQuery()
|
||||
->getArrayResult();
|
||||
return $entities;
|
||||
}
|
||||
|
||||
public function get($id)
|
||||
{
|
||||
$qb = $this->em->createQueryBuilder();
|
||||
|
||||
$entity = $qb->select('u,a,c')
|
||||
->from(User::class, 'u')
|
||||
->leftJoin('u.articles', 'a')
|
||||
->leftJoin('u.comments', 'c')
|
||||
->where('u.id = :uid')
|
||||
->setParameter('uid', $id)
|
||||
->getQuery()
|
||||
->getOneOrNullResult(Query::HYDRATE_ARRAY);
|
||||
|
||||
return $entity;
|
||||
}
|
||||
|
||||
public function create($data)
|
||||
{
|
||||
$entity = $this->hydrator->hydrate($data, new User());
|
||||
try {
|
||||
$this->em->persist($entity);
|
||||
$this->em->flush();
|
||||
} catch (Exception $ex) {
|
||||
return false;
|
||||
}
|
||||
return $entity;
|
||||
}
|
||||
|
||||
public function modify($id, $data)
|
||||
{
|
||||
if (null === ($entity = $this->em->find(User::class, $id))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$entity = $this->hydrator->hydrate($data, $entity);
|
||||
$this->em->persist($entity);
|
||||
$this->em->flush();
|
||||
|
||||
return $entity;
|
||||
}
|
||||
|
||||
public function delete($id)
|
||||
{
|
||||
if (null === ($entity = $this->em->find(User::class, $id))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
$this->em->remove($entity);
|
||||
$this->em->flush();
|
||||
} catch (Exception $ex) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user