diff --git a/lib/Cake/Model/Model.php b/lib/Cake/Model/Model.php index 8909483b877..c95ff26a8df 100644 --- a/lib/Cake/Model/Model.php +++ b/lib/Cake/Model/Model.php @@ -2863,7 +2863,7 @@ protected function _findNeighbors($state, $query, $results = array()) { $query['order'] = $field . ' ASC'; $neighbors = $this->find('all', $query); if (!array_key_exists('prev', $return)) { - $return['prev'] = $neighbors[0]; + $return['prev'] = isset($neighbors[0]) ? $neighbors[0] : null; } if (count($neighbors) === 2) { $return['next'] = $neighbors[1]; diff --git a/lib/Cake/Test/Case/Model/ModelReadTest.php b/lib/Cake/Test/Case/Model/ModelReadTest.php index 58a2ff47232..7209e021672 100644 --- a/lib/Cake/Test/Case/Model/ModelReadTest.php +++ b/lib/Cake/Test/Case/Model/ModelReadTest.php @@ -3736,6 +3736,30 @@ public function testFindNeighbors() { $this->assertEquals($expected, $result); } +/** + * Test find(neighbors) with missing fields so no neighbors are found. + * + * @return + */ + public function testFindNeighborsNoPrev() { + $this->loadFixtures('User', 'Article', 'Comment', 'Tag', 'ArticlesTag', 'Attachment'); + $Article = new Article(); + + $result = $Article->find('neighbors', array( + 'field' => 'Article.title', + 'value' => 'Second Article', + 'fields' => array('id'), + 'conditions' => array( + 'Article.title LIKE' => '%Article%' + ), + 'recursive' => 0, + )); + $expected = array( + 'prev' => null, + 'next' => null + ); + $this->assertEquals($expected, $result); + } /** * testFindCombinedRelations method *