0

我有一个从数据库构建用户对象集合的函数:

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;
}

该方法在底部有一些调试输出,但关键是如果我使用 null groupid 参数调用该方法,即它运行第二个条件,它会打印出我期望接收的集合的一个很好的指示,这很棒。

然而 ..

这是我的调用方法:

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

有趣的是输出:

  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] => kirsty2@softyolk.com
                            [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] => seb@antithug.co.uk
                            [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 条件中简单地返回方法调用的结果,一切都为预期的。

请问有什么想法吗?

谢谢

添加了 UserGroup::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;        
}

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

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 块中,我只会看到问题。

4

2 回答 2

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);
于 2009-08-07T17:10:25.637 回答
0

结果该方法被调用了两次,第二次调用使用了另一个条件,并返回了一个空白集合(问题结果)。

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

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

感谢您的关注

于 2009-08-07T20:31:48.630 回答