Search code examples
phpsymfonydbal

DBAL Symfony 2 SQL Statements


I'm currently playing around with symfony and DBAL to collect data from an external database.

        $conn = $this->get('database_connection');

        $users = $conn->fetchAll('SELECT * FROM cms_clients');

        print_r($users);

The code above works fine, but I need to do more complicated queries, and the only documentation I can find is http://doctrine-dbal.readthedocs.org/en/latest/reference/query-builder.html

The documentation is really good but i'm not sure how i would implement the code bellow in my example.

$queryBuilder ->select('u.id', 'u.name') ->from('users', 'u') ->where('u.email = ?') ->setParameter(0, $userInputEmail) ;

Solution, thanks for replays anyway

      $conn = $this->get('database_connection');
        $parms = array($DataSource, 'STORE');

        $query = "SELECT * FROM cms_things LEFT JOIN cms_stores ON cms_things.id=cms_stores.thing_id WHERE client_id = ? AND type = ? ";

        $users = $conn->fetchAll($query, $parms);

Solution

  • Some thing like this may help

    $qb = $em->createQueryBuilder();
    $params = array('state' => 'active');
    $qb->select("DISTINCT (a.id) as url , a.name , a.pages , a.section , a.clickable")
            ->from('Tbl', 'a')
            ->leftJoin('a.country', 'cnt')
            ->where('a.state=:state')
            ->setMaxResults($limit);
    $qb->setParameters($params);
    $query = $qb->getQuery();
    $list = $query->getArrayResult();