2010-10-08 29 views
0

我想將我的ldap服務器用作數據庫。那麼我將在模型目錄中創建持久化類,擴展到Zend_Ldap,以便我不必編寫所有CRUD操作,但是如何初始化bootstrap.php文件中的ldap連接
,例如,使用學說數據庫連接可以這樣初始化,我想爲LDAP連接如何在zend-framework中的bootstrap.php文件中初始化我的ldap連接,如教條連接

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; 
} 

做相同的,但現在我想要做的是,如果我有一個像這樣(下圖)的一些事情在模型文件夾

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) 

} 

然後在控制器我想要做一些事情如DB

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

我怎麼能初始化模型中的LDAP連接和訪問它,這可能是可能的

回答

1

你可以做

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

但有一個默認 LDAP連接沒有這樣的事情,所以你必須檢索您的引導資源的LDAP連接對象。一些助手類可能會有所幫助。

順便說一句,你的模型不應該延伸Zend_Ldap - 至少不是因爲你想這樣做的原因。你可以例如擴展Zend_Ldap_Node,它是單個LDAP條目的表示,而Zend_Ldap是您正在與之通話的LDAP連接和LDAP服務器的表示。

編輯:

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 
    } 
} 

被告知:這是不是真的國家的最先進的模式,但只是一個簡單的解決問題的方法(如果我理解正確的話)。它允許您在應用程序邏輯中執行以下操作:

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

$oldOne = Application_Ldap::loadEntry('old-one'); 
$oldOne->email = "[email protected]"; 
$oldOne->update(); 
+0

我試過那個。但是你能解釋一下你的答案嗎?當你說「但是沒有這樣的東西作爲默認的LDAP連接時,所以你必須從引導程序的資源中檢索LDAP連接對象,有些輔助類可能會幫助你.........「b/se我想避免我上面指出的行,將它們移動到bootstrap.php文件中,如您所示。即使我已將該行移至bootstrap.php文件已經如何以相同的方式訪問連接異議,如同教條或左右..在模型@Stefan Gehrig – jspeshu 2010-10-10 12:39:02

+0

我的意思是*但沒有這樣的事情作爲默認LDAP連接*是,儘管Doctrine對默認數據庫連接維護靜態全局引用(使用單例),並在生成的模型中使用此引用以允許與數據庫進行交互,而無需每次指定明確的連接,但「Zend_Ldap」本身自己並沒有這樣的概念。它並不是爲基於LDAP的數據設計某種對象關係映射器,表格網關或行網關。它可以與'Zend_Db'在其核心部分進行比較:提供一個接口到... – 2010-10-11 07:13:24

+0

...數據庫連接。我會擴展我的答案以展示一種可能的方法。 閱讀你的編輯,我覺得你應該看看'Zend_Ldap','Zend_Ldap_Node'和'Zend_Ldap_Node_Collection'代表什麼。你在混合這些實體。 – 2010-10-11 07:15:18