$em = $this->getEntityManager();
$query = $em->createQuery(
"SELECT b
FROM AppBundle:Booking b
AND b.checkin IS NULL
AND b.bookingDate=CURRENT_DATE()"
);
I want to see the booking of today, but also i want to have the time of booking in bookindDate. So how to comparison datetime bookingDate with CURRENT_DATE() ?
You can try the following DQL query:
SELECT b
FROM AppBundle:Booking b
AND b.checkin IS NULL
AND b.bookingDate BETWEEN CURRENT_DATE() AND CURRENT_DATE()
Check here the doc for BETWEEN doctrine2 function
Hope this help
yo can use
$date = new DateTime();
$em = $this->getEntityManager();
$query = $em->createQuery(
"SELECT b
FROM AppBundle:Booking b
AND b.checkin IS NULL
AND b.bookingDate=". $date->format('Y-m-d')
);
$em = $this->getEntityManager();
$query = $em->createQuery(
"SELECT b
FROM AppBundle:Booking b
AND b.checkin IS NULL
AND b.bookingDate >= CURRENT_DATE()
AND b.bookingDate <= CURRENT_DATE()+1"
);
this work for me, too easy ;)
Related
Currently I have this DQL query which I want to rewrite with a QueryBuilder, but because I play several entities it is complicated, I have seen examples but I do not understand how more than one entity is related to the code I want to rewrite is the following:
Public function getDepartamentoEmpresaEmpleado($numdoc){
// Muestra todos los departamentos de la empresa a la cual pertenece el empleado logeado
$em = $this->getEntityManager();
$empresa_repo = $em->getRepository('BackendBundle:Empresa');
$idempresa = $empresa_repo->getVerIdempresa($numdoc);
$dql = "SELECT a FROM BackendBundle:DepartamentoRrhh a
INNER JOIN BackendBundle:CentroCosto b WITH a.idcentroCosto = b.idcentroCosto
INNER JOIN BackendBundle:Empresa c WITH b.idempresa = c.idempresa
WHERE c.idempresa = :idempresa";
$query = $em->createQuery($dql)->setParameter('idempresa', $idempresa);
$Departamentos = $query->getResult();
return $Departamentos;
}
Try this,
Add this in the repository of DepartamentoRrhh entity
public function getDepartamentoEmpresaEmpleado($numdoc){
$em = $this->getEntityManager();
$idempresa = $em->getRepository('BackendBundle:Empresa')->getVerIdempresa($numdoc);
$query = $this->createQueryBuilder('a')
->innerJoin('a.idcentroCosto', 'b')
->innerJoin('b.idempresa', 'c')
->where('c.idempresa = :idempresa')
->setParameter('idempresa', $idempresa)
->getQuery();
$Departamentos = $query->getResult();
return $Departamentos;
}
Hope this will work.
This should work
$Departamentos = $empresa_repo->createQueryBuilder('a')
->innerJoin('a.idcentroCosto', 'b')
->innerJoin('b.idempresa', 'c')
->andWhere('c.idempresa = :idempresa')
->setParameter('idempresa', $idempresa)
->getQuery()
->getResult();
There is no need to specify Entity when doing innerJoin because
doctrine already knows about it through the entity mapping.
the goal of this query is to show records from table orders that don't have any shipments created yet. Here is how it should be done in SQL:
SELECT *
FROM orders
LEFT JOIN orders_shipments shipments ON orders.trx_id = shipments.trx_id
WHERE shipments.shipment_id IS NULL
AND orders.purchase_date IS NOT NULL
AND orders.fulfillment_channel = 'MFN';
The following query shows 0 results. Vs the following:
$qb = $this->createQueryBuilder('orders');
$qb->select('orders, shipments')
->leftjoin('orders.shipments', 'shipments')
->Where('shipments.id IS NULL')
->ANDWhere('orders.purchaseDate IS NOT NULL')
->ANDWhere('orders.fulfillmentChannel = :a')->setParameter('a', 'MFN');;
$results = $qb->getQuery()
->getResult();
return $results;
Does show results. Why is that and how to fix it?
Not sure why but I had to use GROUP and HAVING to get it to work:
$qb = $this->createQueryBuilder('orders');
$qb->select('orders, shipments')
->leftjoin('orders.shipments', 'shipments')
->Where('shipments.id IS NULL')
->ANDWhere('orders.purchaseDate IS NOT NULL')
->ANDWhere('orders.fulfillmentChannel = :a')->setParameter('a', 'MFN')
->GroupBy('orders.id')
->having('count(shipments) = 0');
$results = $qb->getQuery()->getResult();
pls help i get this error
Cannot count query which selects two FROM components, cannot make distinction
when i try to break my query to add conditional statement
i have read this
KnpPaginatorBundle/Resources/doc/manual_counting.md and i arrived at this
public function findCategoryProduct($category,$minPrice=null,$maxPrice=null,$gender=null)
{
$countgb = $this->createQueryBuilder('1')
->select('count(p)')
->from('AppBundle:Product','p')
->join('p.group', 'g')
->join('g.category', 'c')
->where('c = :category')
->andWhere('p.visible >= :true')
->setParameter('category', $category)
->setParameter('true', 1);
$count = $countgb->getQuery()->getSingleScalarResult();
$query = $this->createQueryBuilder('1')
->select('p')
->from('AppBundle:Product','p')
->join('p.group', 'g')
->join('g.category', 'c')
->where('c = :category')
->andWhere('p.visible >= :true')
->setParameter('category', $category)
->setParameter('true', 1);
$query ->getQuery()
->setHint('knp_paginator.count', $count);
return $query;
}
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate($query,$request->query->getInt('page', 1),10,array('distinct' => false));
and i still get the error
Hi I advise you to use subquery like :
$query = $em->createQuery('SELECT u.id FROM CmsUser u WHERE EXISTS (SELECT p.phonenumber FROM CmsPhonenumber p WHERE p.user = u.id)');
$ids = $query->getResult();
or with expr:
$query->andWhere($query->expr()->notIn('c.id', $subquery->getDQL()));
some documentation here
I'm trying to do DQL query, but having some troubles with it...
$user = $this->getUser();
$query = $em->createQuery(
'SELECT p
FROM AppBundle:User u
JOIN AppBundle:Follower f
JOIN AppBundle:Photo p
WHERE u.id = :id
AND f.follower_id = :id
AND p.user_id = f.user_id'
)->setParameter('id', $user->getId());
I am trying to get Photos (AppBundle:Photo) of those users, to whom the logged user is following.
Getting next error:
`[Syntax Error] line 0, col 128: Error: Expected =, <, <=, <>, >, >=, !=, got 'p'`
Whats wrong here with 'p' ?
I don't understand why follower are in your code, I don't see relation with photo...
After, I think you call Join but I don't see the relation you made with the photo...
$query = $em->createQuery(
'SELECT p
FROM AppBundle:Photo p
JOIN p.user u
WHERE u.id = :id ')->setParameter('id', $user->getId());
Here is a part of the official doc :
Example:
Regular join of the address:
createQuery("SELECT u FROM User u JOIN u.address a
WHERE a.city = 'Berlin'"); $users = $query->getResult();
Fetch join of the address:
createQuery("SELECT u, a FROM User u JOIN
u.address a WHERE a.city = 'Berlin'"); $users = $query->getResult();
did yu try this?:
$query = $em->createQuery(
'SELECT p
FROM AppBundle:Photo p
JOIN AppBundle:Follower f
JOIN AppBundle:User u
WHERE u.id = :id
AND f.follower_id = :id
AND p.user_id = f.user_id' )->setParameter('id', $user->getId());
I am applying this query for below D6 query , not working ..dont know wat wrong i'm doing ....does innerjoin fails in some condition
$result = db_select('px_slides','s')
->join('node','n','s.vid = n.vid')
->fields('s',array('tissue_type','body_site'))
->fields('n',array('sticky','title'))
->condition('n.status','1','=')
->condition('s.cid','126','=')
->execute()->fetchObject();
drupal 6 query i have:
$result = db_query('
SELECT n.nid, n.vid, n.sticky, n.title, n.created, s.cid, s.ref_id, s.viewurl, s.specimen_type, s.tissue_type, s.body_site, s.test_type, s.algorithm, s.result
FROM {px_slides} s INNER JOIN {node} n ON n.vid = s.vid
WHERE n.status = 1 ')->execute();
You need to put your call to ->join() on a separate line altogether, as it doesn't return the query object:
$query = db_select('px_slides','s')
->fields('s',array('tissue_type','body_site'))
->fields('n',array('sticky','title'))
->condition('n.status','1','=')
->condition('s.cid','126','=');
$query->join('node','n','s.vid = n.vid');
$result = $query->execute()->fetchObject();
The join method does not chain like that. You will have to do something like:
$query = db_select('px_slides','s')
->join('node','n','s.vid = n.vid');
$query->fields('s',array('tissue_type','body_site'))
->fields('n',array('sticky','title'))
->condition('n.status','1','=')
->condition('s.cid','126','=');
$result = $query->execute()->fetchObject();
Also you can use the to string magic method to see the query it is going to execute.
$query->__toString();
Try this...
$query = db_select('px_slides', 's');
$query->innerJoin('node,'n','s.vid = n.vid');
$query->fields('s',array('tissue_type','body_site'));
$query->fields('n',array('sticky','title'));
$query->condition('n.status','1');
$query->condition('s.cid','126');
$result= $query->execute()->fetchAll(PDO::FETCH_ASSOC);