0

我正在检索一堆数据,由于某种原因,一些数据被损坏了。例如,我有一些 Post 模型,每个模型都与 Comment 模型(hasMany)相关,并且每个 Comment 模型都属于一个用户。检索数据时,这是我从数据库中获得的评论:

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

)

每个 Comments[i][User] 数组的第一个字符已被替换为大写 U,尽管在每种情况下它都应该不同(例如 ID 为 18,用户名 Jace 等)。

我将其追溯到我正在使用的数组操作,以在 afterFind() 函数中为 Flex 交互分配一个 _explicitType 字段(谢谢,Paweł Mysior!)。这是我停留在 _explicitType 中的循环:

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

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

我认为它与引用分配有关,但我想不出它为什么会发生。

4

2 回答 2

0

这很奇怪。

在 core.php 中将 debug 设置为 2 并查看页面底部的 sql 日志,也许你会在那里找到一些东西。还要查看所有模型(应用程序、帖子、用户、评论)。可能有一些 beforeFind() 导致这种情况发生。当您执行简单的 User->find() 时也会发生这种情况吗?

顺便提一句。你如何在这里检索数据?

于 2010-02-25T21:17:12.437 回答
0

我认为找到了问题。我在 foreach() 中移动了对数组的检查,现在它似乎工作正常。我认为这是因为在非数组项目上,它实际上破坏了一些东西。这是我在测试用例上记录的更改循环:

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

果然,它记录了用大写 U 代替第一个字母的数据。

于 2010-03-01T21:27:04.157 回答