繁体   English   中英

PHP函数返回值嵌套数组已删除

[英]PHP function return value nested array removed

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

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();

有趣的是输出:

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

这里可能的问题在于您的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);

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

通过在每种情况下设置回显,可以看到它们被调用的情况,首先调用null情况,然后调用non null。

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

谢谢看

暂无
暂无

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

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