1

I want to transcribe this sql statement to doctrine format. It was complicated because I don't know how to perform subqueries. The sentence is as follows:

SELECT * FROM articulo WHERE descripcion = (SELECT descripcion FROM articulo WHERE codigo LIKE 4060981104537)

3 Answers 3

1

You can use Doctrine expression API for subqueries. Example for your query:

you should put following code to Doctrine repository of your articulo entity

$codigo = '4060981104537';
$qb = $this->createQueryBuilder('a');

$qb->where(
  $qb->expr()->in(
    'a.descripcion',
     $this->createQueryBuilder('a2')
       ->select('a2.descripcion')
       ->where('a2.codigo LIKE :codigo')
       ->setParameter('codigo', $codigo)
       ->getDQL()
     )
);
Sign up to request clarification or add additional context in comments.

Comments

1

Do sth like this:

$codigo = 4060981104537;
$expr = $em->getExpressionBuilder();
$em->createQueryBuilder()
   ->select('*')
   ->from('articulo')
   ->where(
       $expr->in(
           'articulo.descripcion',
           $em->createQueryBuilder()
               ->select('*')
               ->from('articulo', 'secondArticulo')
               ->where('secondArticulo.codigo LIKE :codigo')
               )
               ->getDQL()
       )
   )
   ->setParameter('codigo', $codigo);

And for more info look at this link

3 Comments

It still doesn't work, I don't know why 😭
what is the problem? do you get any error?
just when I try to make it work it returns an error: "DataTables warning: table id=DataTables_Table_0 - Ajax error. For more information about this error, please see datatables.net/tn/7"
0

I solved it as follows:

$codigo = '4060981104537';
$qb = $this->createQueryBuilder('a2')
           ->select('a2.descripcion')
           ->where('a2.codigo LIKE :codigo')
           ->setParameter('codigo', $codigo);

$descripcion =  $qb->getQuery()->getResult();   
   
$qb = $this->createQueryBuilder('m')
           ->where('m.descripcion= :descripcion')
           ->setParameter('descripcion', $descripcion);

return $qb->getQuery()->getResult();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.