简体   繁体   English

成员组的Modx revo检查返回一个数组,如何将其转换为用于查询的字符串?

[英]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. 我正在开发modx中的自定义组件,客户端要求按资源组ID搜索它。 I've got snippet pulling in the resource group ID, by using the following code: 通过使用以下代码,我获得了摘录资源组ID的代码段:

// 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: 但是$ group作为数组返回,因此我不能在查询中使用它:

Array ( [0] => 8 )

I've tried calling it like this: return $group[0]; 我试过这样称呼它:return $ group [0];

But that just dumps the first letter 'A' 但这只是转储第一个字母“ A”

Basically I just want to place the value in the Array into a variable and then use it in my search query: 基本上,我只想将Array中的值放入变量中,然后在搜索查询中使用它:

$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! 抱歉,这个问题不是很愚蠢,但我是个菜鸟!

As suggested already, remove the print_r() so $groups will contain an array. 正如已经建议的那样,删除print_r()以便$ groups将包含一个数组。

$group = $user->getUserGroups();

Then adjust your final andCondition() as follows ( :IN is expecting an array): 然后按如下所示调整最终的andCondition():IN需要一个数组):

$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 http://rtfm.modx.com/display/xPDO20/xPDOQuery.where

只是删除print_r - > $group = $user->getUserGroups()然后再次尝试return $group[0];

是的-您将不得不对其进行转换,但是它会返回一个数组,因为任何给定的用户都可以属于多个组,因此如果您的客户决定更改其管理用户的方式,则可能需要为该组预先计划。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM