2009-08-07 47 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);