Added afterFind callback to Models where recursive >= 2. Added tests from 'ezyang'. Closes #5103

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7347 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
mark_story 2008-07-20 17:25:39 +00:00
parent c1bac3358a
commit c286736f69
3 changed files with 73 additions and 3 deletions

View file

@ -682,7 +682,6 @@ class DboSource extends DataSource {
for ($j = 0; $j < $count2; $j++) {
$className = $classNames[$j];
if ($model->alias != $className && !in_array($className, $filtered)) {
if (!in_array($className, $filtering)) {
$filtering[] = $className;
@ -758,6 +757,7 @@ class DboSource extends DataSource {
}
}
}
$this->__filterResults($fetch, $model);
return $this->__mergeHasMany($resultSet, $fetch, $association, $model, $linkModel, $recursive);
} elseif ($type === 'hasAndBelongsToMany') {
$ins = $fetch = array();
@ -876,7 +876,17 @@ class DboSource extends DataSource {
}
return $this->fetchAll($query, $model->cacheQueries, $model->alias);
}
/**
* mergeHasMany - Merge the results of hasMany relations.
*
*
* @param array $resultSet Data to merge into
* @param array $merge Data to merge
* @param string $association Name of Model being Merged
* @param object $model Model being merged onto
* @param object $linkModel Model being merged
* @return void
**/
function __mergeHasMany(&$resultSet, $merge, $association, &$model, &$linkModel) {
foreach ($resultSet as $i => $value) {
$count = 0;

View file

@ -1741,7 +1741,7 @@ class ModelTest extends CakeTestCase {
* @return void
*/
function testAssociationAfterFind() {
$this->loadFixtures('Post', 'Author');
$this->loadFixtures('Post', 'Author', 'Comment');
$TestModel =& new Post();
$result = $TestModel->find('all');
$expected = array(
@ -1757,6 +1757,31 @@ class ModelTest extends CakeTestCase {
)
);
$this->assertEqual($result, $expected);
unset($TestModel);
$Author =& new Author();
$Author->Post->bindModel(array(
'hasMany' => array(
'Comment' => array(
'className' => 'ModifiedComment',
'foreignKey' => 'article_id',
)
)));
$result = $Author->find('all', array(
'conditions' => array('Author.id' => 1),
'recursive' => 2
));
$expected = array(
'id' => 1,
'article_id' => 1,
'user_id' => 2,
'comment' => 'First Comment for First Article',
'published' => 'Y',
'created' => '2007-03-18 10:45:23',
'updated' => '2007-03-18 10:47:31',
'callback' => 'Fire'
);
$this->assertEqual($result[0]['Post'][0]['Comment'][0], $expected);
}
/**
* testValidatesBackwards method

View file

@ -462,6 +462,41 @@ class Comment extends CakeTestModel {
*/
var $hasOne = array('Attachment' => array('dependent' => true));
}
/**
* Modified Comment Class has afterFind Callback
*
* @package cake.tests
* @subpackage cake.tests.cases.libs.model
*/
class ModifiedComment extends CakeTestModel {
/**
* name property
*
* @var string 'Comment'
* @access public
*/
var $name = 'Comment';
var $useTable = 'comments';
/**
* belongsTo property
*
* @var array
* @access public
*/
var $belongsTo = array('Article');
/**
* afterFind callback
*
* @return void
**/
function afterFind($results) {
if (isset($results[0])) {
$results[0]['Comment']['callback'] = 'Fire';
}
return $results;
}
}
/**
* Short description for class.
*