我正在檢索一堆數據,出於某種原因某些數據被破壞。例如,我有一些Post模型,每個模型都與評論模型(hasMany)相關,並且每個評論模型都屬於一個用戶。檢索數據時,這裏就是我從數據庫中獲取的意見:CakePHP 1.2.6/PHP5.2.12數組中的循環錯誤分配引用
[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
)
)
每個評論的第一個字符[I] [用戶]數組已經被替換成一個大寫的U,但在每種情況下它應該是不同的(例如ID爲18,Jace的用戶名等)。
我將其追溯到一個數組操作,我正在爲afterFind()函數中的Flex交互(謝謝,PawełMysior!)分配_explicitType字段。這裏就是我被困在_explicitType循環:
if (is_array($results)) {
foreach ($results as &$item)
{
$item['_explicitType'] = $this->name;
}
} else {
$item[$this->name]['_explicitType'] = $this->name;
}
我認爲它具有與參考分配的事,但我想不出爲什麼它正在發生。
FOR循環似乎在不破壞數組的情況下工作,但在試圖處理沒有數字索引的數組時,它會變得混亂。 –