2009-08-07 105 views
0

我有建立用户对象的集合从所述数据库的函数:PHP函数的返回值嵌套阵列中移除

public static function GetUsersByGroup($instanceID, $groupID) 
{    
    $col = null; 
    if($groupID != null) 
    { 
     $col = UserGroup::GetCollection("User" ,_DB_GET_ALL_INSTANCE_USERGROUP_MEMBERS,array ($instanceID, $groupID)); 
    } 
    else 
    { 
     $col = UserGroup::GetCollection("User" ,_DB_GET_ALL_INSTANCE_NOGROUP_MEMBERS,$instanceID); 
    } 
    echo "this is the collection I am going to return: <pre>"; 
    print_r($col); 
    echo "</pre>"; 
    return $col; 
} 

的方法具有在底部一些调试输出,但问题是,如果我调用方法与一个空的groupid参数即它运行第二个条件,它打印出一个很好的指示我期望收到的集合,这是很好的。

但是..

这里是我的调用方法:

   echo "<br> Collection passed through is: </br>"; 
      $collection = UserGroup::GetUsersByGroup($this->GetInstance()->id,$grouplist->GetCurrentCommandParam()); 
      print_r($collection); 
      $userlist->UpdateCollection($collection); 
      $userlist->DeSelect(); 

的intresting东西是输出:

this is the collection I am going to return: 
Collection Object 
(
    [_valueType:protected] => User 
    [_isBasicType:protected] => 
    [_validateFunc:protected] => 
    [_collection:protected] => Array 
     (
      [0] => User Object 
       (
        [valid] => 
        [validationMessage] => 
        [id] => 29 
        [table:private] => user 
        [fields:private] => Array 
         (
          [title] => mrs 
          [fname] => Kirsty 
          [lname] => Howden 
          [email] => [email protected] 
          [password] => xxxxxxxx 
          [lastlogin] => 2009-07-05 15:20:13 
          [instanceID] => 2 
          [deliveryAddress] => 
          [invoiceAddress] => 
          [tel] => 01752848484 
          [isAdmin] => 0 
          [disabled] => 0 
          [mustAuthorise] => 
          [usergroupID] => 
         ) 

        [validationRules:private] => Array 
         (
         ) 

        [_profileStartTime:protected] => 
        [_profileTag:protected] => 
       ) 

      [1] => User Object 
       (
        [valid] => 
        [validationMessage] => 
        [id] => 31 
        [table:private] => user 
        [fields:private] => Array 
         (
          [title] => master 
          [fname] => Seb 
          [lname] => Howden 
          [email] => [email protected] 
          [password] => xxxxxxxxx 
          [lastlogin] => 2009-07-09 02:02:24 
          [instanceID] => 2 
          [deliveryAddress] => saltash 
          [invoiceAddress] => saltash 
          [tel] => 8908908 
          [isAdmin] => 0 
          [disabled] => 0 
          [mustAuthorise] => 
          [usergroupID] => 
         ) 

        [validationRules:private] => Array 
         (
         ) 

        [_profileStartTime:protected] => 
        [_profileTag:protected] => 
       ) 

     ) 

) 

Collection passed through is: 
this is the collection I am going to return: 
Collection Object 
(
    [_valueType:protected] => User 
    [_isBasicType:protected] => 
    [_validateFunc:protected] => 
    [_collection:protected] => Array 
     (
     ) 

) 
Collection Object ([_valueType:protected] => User [_isBasicType:protected] => [_validateFunc:protected] => [_collection:protected] => Array ()) 

返回的对象已被修改?

如果使用userGroupID调用GetUsersByGroup方法(即第一种情况),则输出全部按预期方式进行。

如果我从方法中删除条件并简单地返回$col = UserGroup::GetCollection("User" ,_DB_GET_ALL_INSTANCE_NOGROUP_MEMBERS,$instanceID);那么所有输出都如预期的那样。

似乎else条件正确执行,然后在返回时被破坏,但只有在else条件存在的情况下才会发生,删除else条件并仅在else条件中返回方法调用的结果,一切都如预期。

有什么想法吗?

感谢

添加的用户组:: GetCollection方法(这是一个深刻的兔子洞虽然可以继续)

protected static function GetCollection($class, $sqlID, $params = null) 
{ 
    $dal = DAL::GetInstance(); //not to be confused with the Instance object, this is an instance of DAL   

    $collection = new Collection($class); 
    $items = $dal->QueryForAssoc($sqlID,$params); 

    foreach($items as $item) 
    { 
      $itemObject = new $class(); 
      $itemObject->LoadFromList($item); 
      $collection->add($itemObject); 
    } 

    return $collection;   
} 

为了进一步澄清follwing工作正常::

public static function GetUsersByGroup($instanceID, $groupID) 
{    
    $col = null; 
    //if($groupID != null) 
    //{ 
     //$col = UserGroup::GetCollection("User" ,_DB_GET_ALL_INSTANCE_USREGROUP_MEMBERS,array ($instanceID, $groupID)); 
    //} 
    //else 
    //{ 
     $col = UserGroup::GetCollection("User" ,_DB_GET_ALL_INSTANCE_NOGROUP_MEMBERS,$instanceID); 
    // } 
    return $col; 
} 

如果该行在else块中,我只会看到问题。

+0

这真的取决于功能用户组:: GetCollection - 我们需要看到的是(甚至更多),以便能够回答这个问题! – Mez 2009-08-07 17:07:32

回答

0

原来的方法被调用两次,第二次呼叫是使用其他条件,并返回一个空集(该问题的结果)。

通过在每个条件中设置一个回声,我可以看到它们被调用时,首先调用null情况,然后是非null。

实际的错误是我有一个有状态列表在同一回发中调用方法两次。很难赶上。

感谢您寻找

1

这里可能存在的问题在于你的UserGroup::GetCollection函数。 PHP 5通过引用传递所有对象,所以如果您正在根据检索这些对象的方式对此例程进行任何修改,那么此修改将在UserGroup::GetCollection完成后继续存在。

我会仔细研究这两个函数调用之间的差异,并确保在UserGroup::GetCollection中没有发生对象变化。

$col = UserGroup::GetCollection("User" ,_DB_GET_ALL_INSTANCE_USERGROUP_MEMBERS,array ($instanceID, $groupID)); 

$col = UserGroup::GetCollection("User" ,_DB_GET_ALL_INSTANCE_NOGROUP_MEMBERS,$instanceID);