【问题标题】:PHP function return value nested array removedPHP函数返回值嵌套数组被移除
【发布时间】:2009-08-07 17:02:42
【问题描述】:

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

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

【问题讨论】:

  • 这真的取决于函数 UserGroup::GetCollection - 我们需要看到(可能更多)才能回答这个问题!

标签: php return-value corrupt truncated


【解决方案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);

【讨论】:

    【解决方案2】:

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

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

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

    感谢观看

    【讨论】:

      猜你喜欢
      • 2011-07-20
      • 2013-05-29
      • 1970-01-01
      • 1970-01-01
      • 2015-04-19
      • 1970-01-01
      • 2013-12-28
      • 1970-01-01
      • 2015-08-01
      相关资源
      最近更新 更多