0

I am working with a LDAP extension, it queries the Active Directory server and returns the result as a Yii2 ArrayDataProvider.

$attributes = Yii::$app->ldap->searchUserByLogin($this->username, ['mail', 'sn', 'givenname']);

A vardump of the returned info is as follows;

yii\data\ArrayDataProvider#1
(
    [key] => null
    [allModels] => [
        0 => [
            'mail' => ‘[email protected]
            'sn' => ‘Surname’
            'givenname' => ‘FirstName’
            'middlename' => null
        ]
    ]
    [modelClass] => null
    [id] => null
    [yii\data\BaseDataProvider:_sort] => null
    [yii\data\BaseDataProvider:_pagination] => null
    [yii\data\BaseDataProvider:_keys] => null
    [yii\data\BaseDataProvider:_models] => null
    [yii\data\BaseDataProvider:_totalCount] => null
    [yii\base\Component:_events] => []
    [yii\base\Component:_eventWildcards] => []
    [yii\base\Component:_behaviors] => null

It was my understanding I could treat the data as objects and access them like;

$attributes->mail

But I get an error that the object isn't found. Then if I try and treat it like a traditional array I get;

Cannot use object of type yii\data\ArrayDataProvider as array yii2

Help would be much appreciated.

Thanks

1 Answer 1

1

Could you show your view/xxx.html code, ArrayDataProvider provide an object, I want to know how you use ArrayDataProvider?

It seems to be possible in your code to have used this code

$attributes['allModels'][0]['mail']

which should be like below

$results = $attributes->getModels();
Sign up to request clarification or add additional context in comments.

4 Comments

ArrayDataProvider provide an array.
I'm using it in the controller only to create / update the user information from the LDAP server. I get the error Cannot use object of type yii\data\ArrayDataProvider as array yii2 when I try to access it like a regular array $user = new User(); $user->username = $this->username; $user->email = $attributes['allModels'][0]['mail']; $user->setPassword($this->password); $user->generateAuthKey(); $user->save();
Could you show var_dump($results = $attributes->getModels());?
I can get it now.$results = $attributes->getModels(); then $user->email = $results[0]['mail']; Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.