DjibJoker
DjibJoker

Reputation: 13

Symfony Query in controller

$query = $em->query("
    SELECT c.id AS id
    FROM collectif c, zone z
    WHERE
        c.zone_id = z.id
    AND z.label = '$zone'
    ANDc.collectif = '$collectif'
");

$c = $query->fetchAll();
$idc = $c['id'];

I have this query that returns a single line, Symfony shows me an error as what variable id undefined

NB: I know that's don't respect the concept of Symfony [MVC] but it's for a particular reason so if someone can tell me how I can resolve this problem

Thank you

Upvotes: 0

Views: 235

Answers (2)

Alvin Bunk
Alvin Bunk

Reputation: 7764

If you'd rather use the results in the assocative way, you can use fetchAssoc() instead:

$c = $query->fetchAssoc();
$idc = $c['id'];

Here is the documentation for reference: http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/data-retrieval-and-manipulation.html#fetchassoc

I'm just giving an alternate way to do this.

Upvotes: 0

Grzegorz Gajda
Grzegorz Gajda

Reputation: 2474

$query->fetchAll() should return numeric array of elements so key id does not exists. You should try $c[0]['id'] to get value.

Upvotes: 2

Related Questions