views:

325

answers:

2

I have a function that builds a collection of user objects from the database:

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

The method has some debug output at the bottom, but the point is if I call that method with a null groupid param i.e it runs the second condition, it prints out a nice indication of the collection that I expected to receive, which is great.

However ..

Here is my calling method:

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

The intresting thing is the output:

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

The object returned has been modified??

If the GetUsersByGroup method is called with a userGroupID i.e the first case, then output is all as expected.

If i remove the conditional from the method and simply return $col = UserGroup::GetCollection("User" ,_DB_GET_ALL_INSTANCE_NOGROUP_MEMBERS,$instanceID); then all output is as expected.

It seems that the else condition executes correctly, and then is corrupted on return, but this only happens if the else condition is present, remove the else condition, and simply return the result of the method call in the else condition, and all is as expected.

Any idea please?

Thanks

ADDED THE UserGroup::GetCollection Method (this is a deep rabbit hole though, could go on)

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

To further clarify the follwing works fine ::

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

I only see the issue if the line is in the else block.

+1  A: 

The likely problem here lies in your UserGroup::GetCollection function. PHP 5 passes all objects by reference, so if you are doing any sort of modification in this routine based on the way you are retrieving these objects, then this modification will persist after UserGroup::GetCollection has finished.

I would examine carefully the differences between these two function calls and make sure there are no object changes happening in UserGroup::GetCollection.

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

vs.

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

Turns out the method is being called twice, the second call is using the other condition, and returning a blank collection (the problem result).

By setting an echo in each condition I could see as they are called, and first the null case is called and then the non null.

The actual error is that I had a stateful list calling the method twice in the same postback. Hard to catch.

Thanks for looking