<?php
namespace App\Repository;
use App\Entity\Device;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method Device|null find($id, $lockMode = null, $lockVersion = null)
* @method Device|null findOneBy(array $criteria, array $orderBy = null)
* @method Device[] findAll()
* @method Device[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class DeviceRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Device::class);
}
public function totalDevices()
{
return $this->createQueryBuilder('d')
->select('count(d.id)')
->getQuery()->getSingleScalarResult();
}
public function getDeviceCountAndPriceByCompany($company='', $owner=''): array
{
$em=$this->getEntityManager();
if ($company!='') {
$query = $em->createQuery('select count(d.id) as deviceCount,dt.price
from App\Entity\Device d
inner join d.deviceType dt
where d.company=:company')
->setParameter('company', $company);
}
if ($owner!='') {
$query = $em->createQuery('select count(d.id) as deviceCount,dt.price
from App\Entity\Device d
inner join d.deviceType dt
where d.owner=:owner')
->setParameter('owner', $owner);
}
return $query->getResult();
}
public function findDeviceBySettingPin($pin)
{
return $this->createQueryBuilder('i')
->orWhere('i.deviceSetting LIKE :pin')
->orWhere('i.deviceSetting LIKE :pin2')
->orWhere('i.deviceSetting LIKE :pin3')
->setParameter('pin', '%"pin":"'.$pin.'"%')
->setParameter('pin2', '%"pin": "'.$pin.'"%')
->setParameter('pin3', '%"pin": '.$pin.'%')
->setMaxResults(1)
->getQuery()
->getResult();
}
public function findBySerial($serial)
{
return $this->createQueryBuilder('d')
->Where('d.serial LIKE :serial')
->setParameter('serial', $serial)
->setMaxResults(1)
->getQuery()
->getScalarResult();
}
// /**
// * @return Device[] Returns an array of Device objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('d')
->andWhere('d.exampleField = :val')
->setParameter('val', $value)
->orderBy('d.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?Device
{
return $this->createQueryBuilder('d')
->andWhere('d.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}