views:

126

answers:

2

I'm working on retrieving a stack of data and for some reason some of the data gets corrupted. For instance, I've got some Post models that each are related to Comment models (hasMany), and each of the Comment models belongsTo a User. When retrieving the data, here's what I get from the database for the comments:

[Post] => Array
(
)

[Comments] => Array
(
    [0] => Array
        (
            [content] => "2010 has definitely been a busy year!"
            [created] => 2010-02-10 13:47:15
            [user_id] => 18
            [post_id] => 1
            [User] => Array
                (
                    [id] => U8
                    [username] => Uace
                    [first_name] => Uace
                )

            [_explicitType] => Comment
        )

    [0] => Array
        (
            [content] => "I can't wait..."
            [created] => 2009-12-10 13:57:36
            [user_id] => 18
            [post_id] => 1
            [User] => Array
                (
                    [id] => U8
                    [username] => Uace
                    [first_name] => Uace
                )

            [_explicitType] => Comment
        )

)

The first character of each of the Comments[i][User] arrays has been replaced with a capital U, though in each case it should be different (such as ID of 18, username of Jace, etc).

I traced it back to an array manipulation I was working with to assign an _explicitType field for Flex interaction (Thanks, Paweł Mysior!) in the afterFind() function. Here's the loop where I stuck in the _explicitType:

if (is_array($results)) {
    foreach ( $results as &$item )
    {
            $item['_explicitType'] = $this->name;

    }
} else {
    $item[$this->name]['_explicitType'] = $this->name;
}

I assume it has to do with the assignment by reference, but I can't think of why it is happening. Any ideas? Thanks!

A: 

It is very strange.

Set debug to 2 in core.php and look in the sql log at the bottom of the page, maybe you'll find something there. Also look through all models (app, post, user, comment). There might be some beforeFind() that are causing this to happen. Does it also happen when you do a simple User->find()?

Btw. how do you retrieve data here?

PawelMysior
Ah, I think I found it. I was doing some stuff with an afterFind(). See modified post above.
Gimli
A: 

I think found the issue. I moved the check for the array inside the foreach() and that seems to be working correctly now. I assume this is because on non-array items, it actually broke things. Here's my altered loop with logging on the test cases:

foreach ( $results as &$item )
{
    if(is_array($item)) {
        $item['_explicitType'] = $this->name;
    } else {
        $copy = $item;
        $copy['_explicitType'] = $this->name;
        $this->log($copy, LOG_DEBUG);
    }
}

And sure enough, it logged the data with capital U's replacing the first letter.

Gimli