mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 11:06:15 +00:00
Fixed bug where associated model's afterfind was not triggered in particular case
This commit is contained in:
parent
77f698dbcd
commit
93639a7c43
3 changed files with 69 additions and 2 deletions
|
@ -1113,7 +1113,7 @@ class DboSource extends DataSource {
|
||||||
* @return array Array of results that have been filtered through $model->afterFind
|
* @return array Array of results that have been filtered through $model->afterFind
|
||||||
*/
|
*/
|
||||||
protected function _filterResults(&$results, Model $model, $filtered = array()) {
|
protected function _filterResults(&$results, Model $model, $filtered = array()) {
|
||||||
$current = current($results);
|
$current = reset($results);
|
||||||
if (!is_array($current)) {
|
if (!is_array($current)) {
|
||||||
return array();
|
return array();
|
||||||
}
|
}
|
||||||
|
|
|
@ -5019,6 +5019,39 @@ class ModelReadTest extends BaseModelTest {
|
||||||
$this->assertEquals($expected, $result[0]['Post'][0]['Comment'][0]);
|
$this->assertEquals($expected, $result[0]['Post'][0]['Comment'][0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* testDeeperAssociationAfterFind method
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testDeeperAssociationAfterFind() {
|
||||||
|
$this->loadFixtures('Post', 'Author', 'Comment', 'Attachment', 'Article');
|
||||||
|
|
||||||
|
$Post = new Post();
|
||||||
|
$Post->bindModel(array(
|
||||||
|
'hasMany' => array(
|
||||||
|
'Comment' => array(
|
||||||
|
'className' => 'ModifiedComment',
|
||||||
|
'foreignKey' => 'article_id',
|
||||||
|
)
|
||||||
|
)));
|
||||||
|
$Post->Comment->bindModel(array(
|
||||||
|
'hasOne' => array(
|
||||||
|
'Attachment' => array(
|
||||||
|
'className' => 'ModifiedAttachment',
|
||||||
|
)
|
||||||
|
)));
|
||||||
|
|
||||||
|
$result = $Post->find('first', array(
|
||||||
|
'conditions' => array('Post.id' => 2),
|
||||||
|
'recursive' => 2
|
||||||
|
));
|
||||||
|
$this->assertTrue(isset($result['Comment'][0]['callback']));
|
||||||
|
$this->assertEquals('Fire', $result['Comment'][0]['callback']);
|
||||||
|
$this->assertTrue(isset($result['Comment'][0]['Attachment']['callback']));
|
||||||
|
$this->assertEquals('Fired', $result['Comment'][0]['Attachment']['callback']);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests that callbacks can be properly disabled
|
* Tests that callbacks can be properly disabled
|
||||||
*
|
*
|
||||||
|
|
|
@ -675,7 +675,6 @@ class MergeVarPluginComment extends MergeVarPluginAppModel {
|
||||||
public $useTable = 'comments';
|
public $useTable = 'comments';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attachment class
|
* Attachment class
|
||||||
*
|
*
|
||||||
|
@ -698,6 +697,41 @@ class Attachment extends CakeTestModel {
|
||||||
public $belongsTo = array('Comment');
|
public $belongsTo = array('Comment');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ModifiedAttachment class
|
||||||
|
*
|
||||||
|
* @package Cake.Test.Case.Model
|
||||||
|
*/
|
||||||
|
class ModifiedAttachment extends CakeTestModel {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* name property
|
||||||
|
*
|
||||||
|
* @var string 'ModifiedAttachment'
|
||||||
|
*/
|
||||||
|
public $name = 'ModifiedAttachment';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* useTable property
|
||||||
|
*
|
||||||
|
* @var string 'attachments'
|
||||||
|
*/
|
||||||
|
public $useTable = 'attachments';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* afterFind callback
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function afterFind($results, $primary = false) {
|
||||||
|
if (isset($results['id'])) {
|
||||||
|
$results['callback'] = 'Fired';
|
||||||
|
}
|
||||||
|
return $results;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Category class
|
* Category class
|
||||||
*
|
*
|
||||||
|
|
Loading…
Add table
Reference in a new issue