Search code examples
ldapsymfonyldap-query

vanilla php ldap query works. Symfony 3 ldap query fails. Why?


I'm trying to figure out how to use the Ldap class in Symfony3. I've successfully created and bound a connection but I can't get any results on a query. To make sure that the query actually works, I ran a bare php version:

    if($lconn = ldap_connect('ds.mydomain.ca')){
        ldap_set_option($lconn, LDAP_OPT_REFERRALS, 0);
        ldap_set_option($lconn, LDAP_OPT_PROTOCOL_VERSION, 3);

        if($lbind = ldap_bind($lconn,'webuser','password')){
            $filter  ="(&(sn=Smith)(givenname=J*))";

            if(!$result = ldap_search($lconn, "dc=ds, dc=mydomain, dc=ca", $filter)) throw \Exception("Error in search query: ".ldap_error($lconn));
            $output = ldap_get_entries($lconn, $result);
        }else{
            $output='bind failed';
        }
    } else {
        $output= 'connection failed';
    }

It returns the expected number of results.

On the other hand, this query done with Symfony 3's Ldap component returns 0 results:

//use Symfony\Component\Ldap\Ldap

$ldap = Ldap::create('ext_ldap', array(
    'host' => 'ds.mydomain.ca',
    'version' => 3,
    'debug' => true,
    'referrals' => false,
));

$ldap->bind('webuser', 'password');
$q = $ldap->query("dc=ds, dc=nrc, dc=ca", "(&(sn=Smith)(givenname=J*))");
$output = $q->execute();

Any idea why the Symfony ldap query fails when all its options should be identical to those I used for the bare php query?


Solution

  • I reposted this question on the Symfony github. @ChadSikorra was there too. And he made it clear what my issue was. Here's his explanation:

    If you look at the collection class, nothing is done with the result resource until initialize() is called in the class. If you do return array('output' => array('bare' => $bare, 'symfony' => $symf->toArray())); it will call initialize and you'll see the entries populated in the class. Unless there's something else going on.