If called without an argument the current (or the new DN if set) DN gets
returned.
If you provide an DN, this entry is moved to the new location specified
if a DN existed.
If the DN was not set, the DN gets initialized. Call {@link update()} to
actually create the new entry in the directory.
To fetch the current active DN after setting a new DN but before an
update(), you can use {@link currentDN()} to retrieve the DN that is
currently active.
/** * Adds a new entry to the directory. * * This also links the entry to the connection used for the add, if it was * a fresh entry. * * @see HordeLdap_Entry::createFresh() * * @param Horde_Ldap_Entry $entry An LDAP entry. * * @throws Horde_Ldap_Exception */ public function add(Horde_Ldap_Entry $entry) { /* Continue attempting the add operation in a loop until we get a * success, a definitive failure, or the world ends. */ while (true) { $link = $this->getLink(); if ($link === false) { /* We do not have a successful connection yet. The call to * getLink() would have kept trying if we wanted one. */ throw new Horde_Ldap_Exception('Could not add entry ' . $entry->dn() . ' no valid LDAP connection could be found.'); } if (@ldap_add($link, $entry->dn(), $entry->getValues())) { /* Entry successfully added, we should update its Horde_Ldap * reference in case it is not set so far (fresh entry). */ try { $entry->getLDAP(); } catch (Horde_Ldap_Exception $e) { $entry->setLDAP($this); } /* Store that the entry is present inside the directory. */ $entry->markAsNew(false); return; } /* We have a failure. What kind? We may be able to reconnect and * try again. */ $error_code = @ldap_errno($link); if ($this->errorName($error_code) != 'LDAP_OPERATIONS_ERROR' | !$this->_config['auto_reconnect']) { /* Errors other than the above are just passed back to the user * so he may react upon them. */ throw new Horde_Ldap_Exception('Could not add entry ' . $entry->dn() . ': ' . ldap_err2str($error_code), $error_code); } /* The server has disconnected before trying the operation. We * should try again, possibly with a different server. */ $this->_link = false; $this->_reconnect(); } }
/** * Writes an LDIF file that describes an entry. * * @param Horde_Ldap_Entry $entry * * @throws Horde_Ldap_Exception */ protected function _writeEntry($entry) { // Fetch attributes for further processing. $entry_attrs = $entry->getValues(); // Sort and put objectclass attributes to first position. if ($this->_options['sort']) { ksort($entry_attrs); if (isset($entry_attrs['objectclass'])) { $oc = $entry_attrs['objectclass']; unset($entry_attrs['objectclass']); $entry_attrs = array_merge(array('objectclass' => $oc), $entry_attrs); } } // Write data. if (!$this->_versionWritten) { $this->writeVersion(); } $this->_writeDN($entry->dn()); foreach ($entry_attrs as $attr_name => $attr_values) { $this->_writeAttribute($attr_name, $attr_values); } $this->_finishEntry(); }