S
S
Sergey Miller2021-02-04 14:54:11
MODX
Sergey Miller, 2021-02-04 14:54:11

Modx Revo API how to parse array of received users?

This is how I define the desired user group (there are only 4 of them)

$group = $modx->getObject('modUserGroup', array ('id' => '2'));
$users = $group->getMany('UserGroupMembers');

Now in the array $userswe have users from the group we need. Did a check count($users)showed 4 - 4 users in this group. Correctly.

Now I'm trying to parse the array by users.

foreach ($users as $user){

// получаем имя и id пользователя
$name = $user->get('username'); //name
$uid = $user->get('id'); //id

// связь с профилем пользователя
$profile = $user->getOne('Profile');
$isUserBlocked = $profile->get('blocked'); // заблокирован ли юзер
$extendedFields = $profile->get('extended'); // доп поля
$age = $extendedFields['age']; // возраст получили из доп полей
$unitsAge = $modx->runSnippet("units", array('input' => $age, 'options' => 'год|года|лет')); //компонент units
// выводим данные
if(!preg_match("/(.*?)admin(.*)/iu", $name)){ //не выводим админа
if($isUserBlocked != '1'){ // не выводим заблокированых юзеров
echo '
<div class="usersforu boxs">
<div class="ufuName">'.$name.'</div>
<div class="ufuAge">Возраст: '.$age.' '.$unitsAge.'</div>

...

</div>
';
}
}



//end for each
}

The problem is that nothing is output from the loop, there are no errors. Is there any other way to parse the array?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Miller, 2021-02-06
@blackseabreathe

Did it like this:

$group = $modx->getObject('modUserGroup', array ('id' => '5'));
$members = $group->getMany('UserGroupMembers');

foreach ($members as $member){
$user = $member->getOne('User'); // важно
// получаем имя пользователя
$name = $user->get('username');
$uid = $user->get('id');
// связь с профилем пользователя
$profile = $user->getOne('Profile');

....

}

A
Arthur Sh, 2021-02-04
@Shev_Art_V

It's very simple, $user is not the object you expect. The UserGroupMembers object is associated with the modUser object via the member field, so you need to do this

foreach ($users as $user){

// получаем id пользователя
$uid = $user->get('member'); 
// получаем объект пользователя
$user = $modx->getObject('modUser', $uid);
// получаем имя пользователя
$name = $user->get('username');
// получаем профиль пользователя
$profile = $user->getOne('Profile');
/****тут остальной код*****/
//end for each
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question