Search code examples
phparraysmodx

Modx revo check on member group returns an array, how do I convert it to a string for use in a query?


I'm developing a custom component in modx, the client has asked that it be searched by resource group id. I've got snippet pulling in the resource group ID, by using the following code:

// Check if user is logged in and get user group 
$user = (!empty($userId)) ? $modx->getObject('modUser', $userId) : $modx->user;

if (is_object($user)) {
    /*
    * modUser Groups
    */
    $group = print_r($user->getUserGroups(), true);
    $modx->toPlaceHolder('user.groups', $group);

} 

However $group returns as an array, so I can't use it in the query:

Array ( [0] => 8 )

I've tried calling it like this: return $group[0];

But that just dumps the first letter 'A'

Basically I just want to place the value in the Array into a variable and then use it in my search query:

$query->where(array('accNumber:LIKE' => "%$accNo%"));
$query->andCondition(array('accName:LIKE' => "%$accName%"));
$query->andCondition(array('saleProperty:LIKE' => "%$saleProperty%"));
$query->andCondition(array('clientNumber' => "$group")); 

Am I right in thinking that I'll need to convert the contents of the array into a string before I can use them? Sorry if the question is noobish, but I'm a noob!


Solution

  • As suggested already, remove the print_r() so $groups will contain an array.

    $group = $user->getUserGroups();
    

    Then adjust your final andCondition() as follows (:IN is expecting an array):

    $query->where(array('accNumber:LIKE' => "%$accNo%"));
    $query->andCondition(array('accName:LIKE' => "%$accName%"));
    $query->andCondition(array('saleProperty:LIKE' => "%$saleProperty%"));
    $query->andCondition(array('clientNumber:IN' => $group));
    

    http://rtfm.modx.com/display/xPDO20/xPDOQuery.where