how to use IN in WHERE claus doctrine 2 using querybuilder - doctrine-orm

How can I use IN in WHERE claus in doctrine2 using querybuilder
How can I convert following sql in doctrine using querybuilder?
SELECT fooid FROM foo WHERE bar IN ('A','C');

All is included in the Doctrine documentation.
Assuming $qb is your query builder:
$qb->add('select', new Doctrine\ORM\Query\Expr\Select(array('Foo.fooid')))
->add('from', new Doctrine\ORM\Query\Expr\From('Foo'))
->add('where', $qb->expr()->in('Foo.bar', '?1'))
->setParameter(1, array('A', 'C'));

It can also be written using
$qb->select('Foo.fooid')
->from('Foo')
->where($qb->expr()->in('Foo.bar', '?1'))
->setParameters(array(1 => array('A', 'C')));
or as Guillaume mentioned above
$qb = $objectManager->createQueryBuilder();
$qb->add('select', new \Doctrine\ORM\Query\Expr\Select(array('Foo.fooid')))
->add('from', new \Doctrine\ORM\Query\Expr\From('Foo'))
->add('where', $qb->expr()->in('Foo.bar', '?1'))
->setParameter(1, array('A', 'C'));

Related

How to add a property to an object returned by Doctrine

I am doing the following Query in doctrine:
$result = $conductores
->select('c.id, c.runFormateado, c.nombres, c.apellidos, c.requisitosWebcontrol, e.nombre as empresa, c.createdAt, c.updatedAt')
->innerJoin('c.empresa', 'e', 'ON')
->getQuery()
->getResult();
I need to modify each record of that result set to have 2 more properties that I will use later.
I tried by adding "null as new_property" in the select method call but doctrine shows an error.
How can I do it?
Regards
Jaime
You can try something like this:
$result = $conductores
->select('c.id, c.runFormateado, c.nombres, c.apellidos, c.requisitosWebcontrol, e.nombre as empresa, c.createdAt, c.updatedAt, \'\' as new_property_1, \'\' as new_property_2')
->innerJoin('c.empresa', 'e', 'ON')
->getQuery()
->getResult();
You can try this using MySQL directly in your database, to see how this works.
Example: Assuming you have a User table
select Id, FirstName, LastName, '' as new_colum_1, '' as new_column_2
from Users;
I hope this works for you.

Disable lazy loading doctrine zend framework?

How can I disable lazy loading in doctrine 2?
$em = $this->getEntityManager();
$repo = $em->getRepository('Application\Entity\StudentClass');
$result = $repo->findBy(array('pkStudentClass' => '1' ));
print_r($result);
I'm getting here too many data and the script fails.
Please use following query for the data retrieve
You can retrieve here needed column and retrieve needed records.
$query = $this->getEntityManager()->createQueryBuilder()
->select('U.id,U.name')
->from('Application\Entity\StudentClass', 'U')
->where('U.pkStudentClass = :pkStudentClass')
->setParameter('pkStudentClass', 1)
->setMaxResults(20);
->orderBy('id', 'DESC')
->getQuery();
$result = $query->getScalarResult();

Doctrine queryBuilder setParamater

I'm having a problem using Parameters in the Doctrine queryBuilder.
Here's my code:
$queryBuilder
->select('id', 'value')
->from('test')
->where('id = :id')
->setParameter('id', '1', 'integer')
;
This creates:
SELECT id, value FROM test WHERE id = :id
However the setParameter is not applied when I use
$stmt = $conn->query($queryBuilder);
Getting this Error:
"Fatal error: Uncaught exception 'Doctrine\DBAL\Driver\Mysqli\MysqliException' with message 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':id'..."
What am I missing?
Using this works, but I would prefer to integrate the parameters directly in the queryBuilder:
$stmt = $conn->prepare($queryBuilder);
$stmt->bindValue("id", "1", "integer");
$stmt->execute();
Any hints would be great.
It's possible to directly execute the querybuilder, this will attach the paramaters correctly.
Example:
$queryBuilder = $conn->createQueryBuilder();
$queryBuilder
->select('id', 'value')
->from('test')
->where('id = :id')
->setParameter('id', '1', 'integer');
$queryBuilder->execute();

Doctrine Query builder for simple search

Hi I am trying to write a simple search query using QueryBuilder. I have a single table with fields: Name, Description, Code.
Basically I want to check if an entered keyword is in any of the fields.
public function searchProducts( $keyword )
{
$qb = $this->productRepository->createQueryBuilder('u');
$qb->add('where' , 'u.name LIKE :search');
$qb->add('where' , 'u.description LIKE :search');
$qb->add('where' , 'u.code LIKE :search');
$qb->setParameter('search', '%'.$keyword.'%');
}
How do I add the orX context to this?
public function searchProducts( $keyword )
{
$qb = $this->productRepository->createQueryBuilder('u');
$qb->add('where', $qb->expr()->orX(
$qb->expr()->like('u.name', ':search'),
$qb->expr()->like('u.description', ':search'),
$qb->expr()->like('u.code', ':search')
))
$qb->setParameter('search', '%'.$keyword.'%');
}
I think this should work for you.
Additional I'm not sure, but isnt there a select/from missing like this:
$qb->add('select', 'u')
->add('from', 'User u')
I have taken it from the doctrine2 ORM Doc:
Docu

doctrine 2 query builder and join tables

I'm trying to get all comments for each post in my home page
return
$this->createQueryBuilder('c')
->select('c')
->from('Sdz\BlogBundle\Entity\Commentaire' ,'c')
->leftJoin('a.comments' ,'c')->getQuery()->getResult() ;
but I'm getting this error
[Semantical Error] line 0, col 58 near '.comments c,': Error:
Identification Variable a used in join path expression but was not defined before.
PS : The mapping is correct because I can see the page article with its comments.
In case this is still giving you problems, here is your query using the syntax found in the examples in the Doctrine 2.1 documentation.
I'm assuming your query resides in a custom repository method, and that 'a' is an abbreviation for 'Article'.
$em = $this->getEntityManager();
$qb = $em->createQueryBuilder();
$qb->select(array('a', 'c'))
->from('Sdz\BlogBundle\Entity\Article', 'a')
->leftJoin('a.comments', 'c');
$query = $qb->getQuery();
$results = $query->getResult();
return $results;