PHP ldap_mod_replace失败«类型或值存在»错误


PHP ldap_mod_replace fails with «Type or value exists» error

我已经通过PHP的API为LDAP创建了一个轻量级的模型管理器,以简化activedirectory中的对象管理。

一切运行良好,但我有一个问题,当更新多值属性,即使我改变所有的值,事务失败与«类型或值存在»错误,属性在数据库中没有改变。

我使用的测试用例是更改用户的多值"描述"字段。如果我添加新值或更改整个值数组,事务总是失败。

这部分代码如下:

    if (count($mod_attr) > 0)                                                                                                                                                                                                             
    {                                                                                                                                                                                                                                     
        $ret = @ldap_mod_replace($this->getHandler(), $dn, $mod_attr);                                                                                                                                                                    
        if ($ret === false)                                                                                                                                                                                                               
        {                                                                                                                                                                                                                                                 $this->log(sprintf("LDAP ERROR '%s' -- Modifying {%s}.", ldap_error($this->getHandler()), print_r($mod_attr, true)), 'SlapOM'LoggerInterface::LOGLEVEL_CRITICAL);                                                             
            throw new LdapException(sprintf("Error while MODIFYING values <pre>%s</pre> in dn='%s'.", print_r($mod_attr, true), $dn), $this->getHandler(), $this->error);                                                                 
        }                                                                                                                                                                                                                                 
        $this->log(sprintf("Changing attribute {%s}.", join(', ', array_keys($mod_attr))));                                                                                                                                               
    }                                                                                       

完整的代码可以在这里找到[在github](https://github.com/chanmix51/SlapOM/blob/master/lib/SlapOM/Connection.php#L115 [github])。

日志显示如下行:

   2013-06-04 10:39:54 |                => MODIFY dn='CN=HUBERT Gregoire,OU=...
   2013-06-04 10:39:54 | => LDAP ERROR 'Type or value exists' -- Modifying {Array
(   
    [description] => Array
        (   
            [0] => Description 2
            [1] => Description 3
        )
)}

即使前面的值是["description" => ['Description 1']]。有什么我没有得到或做错了吗?

答案很简短:«Description不是一个多值字段»。和往常一样,错误信息是如此令人困惑,它导致我花了几个小时在错误的问题上。

简而言之:LDAP错误20«类型或值存在»可能是您试图在多值字段中插入两次相同的值,或者您试图在单个值字段中插入多个值。