5

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);
Cœur
  • 37,241
  • 25
  • 195
  • 267
Brent
  • 2,385
  • 10
  • 39
  • 63

1 Answers1

3

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();
HMagdy
  • 3,029
  • 33
  • 54
  • Error: Call to undefined method Doctrine\DBAL\Query\QueryBuilder::getQuery() in – Brent Mar 12 '14 at 10:24
  • you can find public function getQuery() https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/QueryBuilder.php – HMagdy Mar 12 '14 at 10:27
  • maybe different version of doctrine ... what's your version? – HMagdy Mar 12 '14 at 10:44
  • $parms = array($DataSource, 'STORE'); /* LEFT JOIN cms_stores ON cms_things.id=cms_stores.thing_id WHERE client_id */ $query = "SELECT * FROM cms_things LEFT JOIN cms_stores ON cms_things.id=cms_stores.thing_id WHERE client_id = ? AND type = ? "; – Brent Mar 12 '14 at 11:42