views:

33

answers:

1

i want to use my ldap server as a DB. then i will create persistence classes in the models directory that will extend to Zend_Ldap so that i won't have to write all the CRUD operations but how can i initialize the ldap connection in the bootstrap.php file
for e.g. a database connection using doctrine can be initialized like this, i want to do the same for ldap connection

protected function _initDoctrine()
 {
    $autoloader = Zend_Loader_Autoloader::getInstance();
    $autoloader->registerNamespace('Doctrine');
    $this->getApplication()->getAutoloader()
        ->pushAutoloader(array('Doctrine', 'autoload'));
    spl_autoload_register(array('Doctrine', 'modelsAutoload'));

    $manager = Doctrine_Manager::getInstance();
    $doctrineConfig = $this->getOption('doctrine');
    $manager->setAttribute(Doctrine::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
    $manager->setAttribute(Doctrine::ATTR_AUTOLOAD_TABLE_CLASSES, true);

    Doctrine_Core::loadModels($doctrineConfig['models_path']);

    $conn = Doctrine_Manager::connection($doctrineConfig['dsn'],'doctrine');
    $conn->setAttribute(Doctrine::ATTR_USE_NATIVE_ENUM, true);
 return $conn;
}

but now what i want to do is if i have some thing like this(below) in the models folder

class Application_Model_entry extends Zend_Ldap_Node_collection {

public static function getInstance() {
    $options = Zend_Registry::get('config')->ldap;//i want to avoid this line
    $ldap = new Zend_Ldap($options); // i want to avoid this line
    $dn = $ldap->getBaseDn(); // i want to avoid this line

    $a = new Zend_Ldap_Node_Collection(new Zend_Ldap_Collection_Iterator_Default($ldap,'email')); //also this one 
    return $a;//where $a is an instance of an LDAP entry(node)

}

then in the controller i want to do some thing like a db

$ent = new Application_Model_entry();
$ent->email = "[email protected]";
...
$ent->save();
$ent->update();

how can i initialize the ldap connection and access it in the models so that this could be possible

+1  A: 

You can do

protected function _initLdap() 
{
    $ldap = new Zend_Ldap(/*... your configuration ...*/);
    return $ldap;
}

But there is no such thing as a default LDAP connection, so you have to retrieve the LDAP connection object from your bootstrap's resources. Some helper class may, well, help.

By the way your model should not extend Zend_Ldap - at least not for the reason you want to do this. You could e.g. extend Zend_Ldap_Node which is a representation of a single LDAP entry, whilst Zend_Ldap is a representation of the LDAP connection and the LDAP server you're talking to.

EDIT:

class Application_Ldap
{
    public static function getLdap()
    {
        /* return LDAP connection from bootstrap */
    }

    public static function newEntry($name) 
    {
        $dn   = $name; // build DN from given entity name
        $node = Zend_Ldap_Node::create($dn);
        $node->attachLdap(self::getLdap());
        return $node;
    }

    public static function loadEntry($name)
    {
        $dn   = $name; // build DN from given entity name
        $node = Zend_Ldap_Node::fromLdap($dn, self::getLdap());
        return $node
    }
}

Be advised: this is not really a state-of-the-art model, but just a simple solution to your problem (if I understood it correctly). It allows you to do the following in your application logic:

$newOne = Application_Ldap::newEntry('new-one');
$newOne->email = "[email protected]";
$newOne->update();

$oldOne = Application_Ldap::loadEntry('old-one');
$oldOne->email = "[email protected]";
$oldOne->update();
Stefan Gehrig
i have tried that one. but could you explain your answer i didn't get you when you say "But there is no such thing as a default LDAP connection, so you have to retrieve the LDAP connection object from your bootstrap's resources. Some helper class may, well, help......... " b/se i want to avoid the lines i indicated above by moving them to the bootstrap.php file as you indicated. even though i moved that line to the bootstrap.php file already how can i access the connection objection in same way as doctrine or so .. in the models @Stefan Gehrig
jspeshu
What I meant by *But there is no such thing as a default LDAP connection* is, that while Doctrine maintains a static global reference (using a singleton) to the default database connection and uses this reference inside the generated models to allow interaction with the database without specifying a distinct connection each time, `Zend_Ldap` itself does not have such a concept by its own. It was not designed to be some kind of object-relational-mapper, table-gateway or row-gateway for LDAP-based data. It can be compared to what `Zend_Db` does in its core: provide an interface into the...
Stefan Gehrig
... database connection. I'll extend my answer to show a possible approach.Reading your edits, I tink you should have a depper look at what `Zend_Ldap`, `Zend_Ldap_Node` and `Zend_Ldap_Node_Collection` represent. You're mixing up these entities.
Stefan Gehrig
thank you that is better
jspeshu