QueryBuilder generates Error: Expected Literal, got 'SELECT'

0

How are you?

I am developing a program with symfony 3.4, xampp and mariadb 10. I must make a query with a nested subquery: I must go through an APPLICATION table looking for if there is not a certain PERSON that I am looking for with your ID with an existing application. I have solved it using the following code:

 $subconsulta = 'SELECT p.id FROM ComensalesBundle:Persona p where p.dni =  ' . $Dni ;
$consulta = 'SELECT s FROM ComensalesBundle:Solicitud s where s.persona = (' .$subconsulta. ')';

I can not get it to work using the Doctrine QueryBuilder, I use the following code:

$db = $this->getDoctrine()->getEntityManager();
    $qb = $db->createQueryBuilder();
    $subqb = $qb;
    //escribo la subconsulta
    $subqb =  $subqb->select('p.id')
            ->from('ComensalesBundle:Persona','p')
            ->where('p.dni = ' . $Dni)
            ->getDQL()

            ;
    //$resultado = $subqb->getResult();
    //var_dump($subqb);
    //escribo la consuta
    $q = $qb->select('s')
       ->from('ComensalesBundle:Solicitud','s')
       ->where('s.persona = ' . $subqb)
       ->getQuery()
            ;

    $resultado = $q->getResult();

I get the following error: [Syntax Error] line 0, col 89: Error: Expected Literal, got 'SELECT' I guess I'm putting the query wrong but after searching forums I can not give a solution. I am a user with little experience and I am in everything progressing in what is web programming, any contribution will be welcome. Thank you Greetings

    
asked by Cristian Budzicz 26.05.2018 в 00:02
source

1 answer

0

You can try using NativeQuery

$sql1 = "select id from persona where p.dni =  ?";
$sql2 = "select * from solicitud where persona_id = " . $sql1;

$conn = $em->getConnection();
$stmt = $conn->prepare($sql);
$stmt->bindValue(1, '4325xxxx');
$stmt->execute();
$rows = $stmt->fetchAll();

From the controller (which I do not recommend)

$entityManager = $this->getDoctrine()->getManager();

If you are using a Repository

$em = $this->getEntityManager();

Slds

    
answered by 26.05.2018 / 04:27
source