Excluding elemens via NOT IN Symfony2 - symfony

I am trying to create a query in Symfony 2.3 which should exclude some elements based on the ID in another table, for example I have an SMS table, and a SMS_Deleted table, I need to exclude ids from SMS_Deleted from the query.
I have this first query to get the SMS deleted:
$sms_exceptions = $this->getDoctrine()
->getEntityManager()
->createQueryBuilder()
->select(array('s.idClient'))
->from('AloBundle:SmsDeleted', 's')
->getQuery()
->getResult();
in $sms_exceptions I get all ids should be excluded from the SMS query. Then in the SMS Repository I have this:
public function findByClientArray($id_client, $sms_exceptions, $from, $to, $jtStartIndex, $jtPageSize)
{
$qb = $this->getEntityManager()->createQueryBuilder();
return $this->getEntityManager('voipswitch')
->createQueryBuilder()
->select(array('s.idClient, s.sentTime as date, s.toNumber as destiny, s.smsStatus as status, s.smsText as sms'))
->from('VoipswitchBundle:SmsOutbox', 's')
->where('s.idClient = :id_client AND s.sentTime >= :from AND s.sentTime <= :to')
->where($qb->expr()->notIn('s.idSms', $sms_exceptions))
->setParameter('id_client', $id_client)
->setParameter('from', new \DateTime($from))
->setParameter('to', new \DateTime($to))
->where($qb->expr()->notIn('s.idSms', $sms_exceptions))
->orderBy('s.sentTime', 'DESC')
->setFirstResult($jtStartIndex)
->setMaxResults($jtPageSize)
->getQuery()
->getArrayResult();
}
Any help please???
Thanks

I'd do something like this:
$queryBuilder = $this->getEntityManager()->createQueryBuilder();
$query = $queryBuilder
->select(['f'])
->from('AcmeDemoBundle:FirstEntity', 'f')
->leftJoin('AcmeDemoBundle:SecondEntity', 's', 'WITH', 'f.sId = s.id')
->where($queryBuilder->expr()->isNull('s.id'))
...
...
or with association:
$queryBuilder = $this->getEntityManager()->createQueryBuilder();
$query = $queryBuilder
->select(['f'])
->from('AcmeDemoBundle:FirstEntity', 'f')
->leftJoin('f.secondEntity(ies)', 's')
->where($queryBuilder->expr()->isNull('s.id'))
...
...

Related

Foreach on getRepository Symfony 3

On my controler I get some results from database
$commandes = $em->getRepository('SWPlatformBundle:Vente')->getCommandesAFacturer($client[0]->getId());
with this SQL
public function getCommandesAFacturer($id) {
$qb = $this->createQueryBuilder('v')
->select("c.id, c.montantTTC, c.horodatageCreation, SUM(v.prixTotalVente) AS sansFrais")
->leftJoin('v.commande', 'c')
->where('c.facture IS NULL')
->andwhere('c.commercant = :commercantId')
->setParameter('commercantId', $id)
->groupBy('c.id');
return $qb
->getQuery()
->getResult();
}
And I would like to sum the sansFrais value of my request but I don't reach the sansFrais Value.
I've tried like this, but it doesn't work :
foreach($commandes as $commande){
$montantHTCollecte += $commande['sansFrais'];
}
Thanks for your help !

doctrine pagination with join statement

I am trying to right a query which return me list of the users which has uploaded video(user_id in video table ), and have paginating thingy in the query
the function is like this :
public function getUsersHasVideoShoutOut($offset, $limit)
{
$qb = $this->createQueryBuilder('u')
->Join('u.video', 'uv');
$qb->where('uv.muted=0')
->andwhere('u.muted = 0')
->addOrderBy('uv.release_date', 'DESC')
->setFirstResult($offset)
->setMaxResults($limit);
return $qb->getQuery()->getResult();
}
but the problem is that I get duplicate data in next pages , is it because of join statement and pagination in doctorine ?
You can get distinct data:
public function getUsersHasVideoShoutOut($offset, $limit)
{
$qb = $this->createQueryBuilder('u')
->Join('u.video', 'uv');
$qb->where('uv.muted=0')
->andwhere('u.muted = 0')
->addOrderBy('uv.release_date', 'DESC')
->setFirstResult($offset)
->setMaxResults($limit)
->distinct();
return $qb->getQuery()->getResult();
}

How QueryBuilder's select work?

I have a Category that has many Marker. I want to get a category by id with all the markers associated.
I have this query in my Marker's repository:
public function findOneByIdJoinedToCategory($id)
{
return $this->createQueryBuilder('m')
->innerjoin('m.category', 'c')
->where('c.id = :id')
->setParameter('id', $id)
->getQuery()
->getResult()
;
}
It works as intended, i get the category's data with an array marker with all the marker data. But I don't want all the data field so I tested this query:
public function findOneByIdJoinedToCategory($id)
{
return $this->createQueryBuilder('m')
->select('m.id', 'm.name', 'm.description', 'm.vicinity', 'm.geo')
->innerjoin('m.category', 'c')
->addSelect('c.id', 'c.name')
->where('c.id = :id')
->setParameter('id', $id)
->getQuery()
->getResult()
;
}
When I do this, I except to have only the field I selected, but I have an extra array and get this error : Call to a member function getCategory() on array
How do I select my field and get a category with all its marker ?
I think you need to use Doctrine partial objects here
So select will look something like this
->select('partial m.{id, name, description...

Symfony 3- select query

I'm new to symfony and I'm having a hard time developing a query ... thanks if anyone could help me
What I need is
select id and code from table where id = userid and code = usercode
if the values ​​are equal if statement to insert data into the database, otherwise error message
i try this
$teste = $this->getDoctrine()->getRepository('AppBundle:Codigo');
$query = $teste->createQueryBuilder('p')
->where('p.codigo = :codigo')
->andWhere('p.utilizadorID = :user')
->setParameter('codigo', $codigo)
->setParameter('user', $user)
->getQuery();
}
$cod = $query->getResult();
if ( $cod <> NULL) {
$link = $data['linkpodcast'];
$podcast->setUrl($link);
$podcast->setIdUser($user);
$sn = $this->getDoctrine()->getManager();
$sn -> persist($podcast);
$sn -> flush();
$this->addFlash(
'notice',
'Podcast Adicionado'
);
return $this->redirectToRoute('gravar_list');
}

How to return my last count id queryBuilder

Hi i'm new on symfony be indulgent plz, here's my code
public function countTicket()
{
$qb = $this
->createQueryBuilder('t')
->innerJoin('t.visitors', 'v')
->Select("count(v.id)")
->where('t.visitDate IS NOT NULL')
->groupBy('t.visitDate')
;
return $qb
->getQuery()
->getScalarResult();
}
When i dump countTicket() it returns all my Id's entity, group in all visitDate which is normal but i just want to return the count (v.id) from the last visitDate i submit and i don't know how to do that.
Thanks for your help.
You should use getSingleScalarResult() instead of getScalarResult(), and limit your query to fetch the last result only, like this:
public function countTicket()
{
return $this
->createQueryBuilder('t')
->innerJoin('t.visitors', 'v')
->select('count(v.id)')
->where('t.visitDate IS NOT NULL')
->orderBy('t.visitDate', 'desc')
->groupBy('t.visitDate')
->setMaxResults(1)
->getQuery()
->getSingleScalarResult();
}

Resources