mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Fix different format of $results in afterFind
Refs #2529 As of this commit, we can get consistent format of $resutls in afterFind. And we can keep backward compatibility if Model::$useConsistentAfterFind is set to false.
This commit is contained in:
parent
c227c14bf2
commit
c246695518
4 changed files with 96 additions and 3 deletions
|
@ -1399,10 +1399,15 @@ class DboSource extends DataSource {
|
||||||
$this->_mergeAssociation($row, $merge, $association, $type);
|
$this->_mergeAssociation($row, $merge, $association, $type);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
if (!$prefetched && $LinkModel->useConsistentAfterFind) {
|
||||||
|
if ($queryData['callbacks'] === true || $queryData['callbacks'] === 'after') {
|
||||||
|
$this->_filterResultsInclusive($assocResultSet, $Model, array($association));
|
||||||
|
}
|
||||||
|
}
|
||||||
$this->_mergeAssociation($row, $assocResultSet, $association, $type, $selfJoin);
|
$this->_mergeAssociation($row, $assocResultSet, $association, $type, $selfJoin);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($type !== 'hasAndBelongsToMany' && isset($row[$association]) && !$prefetched) {
|
if ($type !== 'hasAndBelongsToMany' && isset($row[$association]) && !$prefetched && !$LinkModel->useConsistentAfterFind) {
|
||||||
$row[$association] = $LinkModel->afterFind($row[$association], false);
|
$row[$association] = $LinkModel->afterFind($row[$association], false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -603,6 +603,25 @@ class Model extends Object implements CakeEventListener {
|
||||||
|
|
||||||
// @codingStandardsIgnoreEnd
|
// @codingStandardsIgnoreEnd
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If true, afterFind will be passed consistent formatted $results in case of $primary is false.
|
||||||
|
* The format will be such as the following.
|
||||||
|
*
|
||||||
|
* {{{
|
||||||
|
* $results = array(
|
||||||
|
* 0 => array(
|
||||||
|
* 'ModelName' => array(
|
||||||
|
* 'field1' => 'value1',
|
||||||
|
* 'field2' => 'value2'
|
||||||
|
* )
|
||||||
|
* )
|
||||||
|
* );
|
||||||
|
* }}}
|
||||||
|
*
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
public $useConsistentAfterFind = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The ID of the model record that was last inserted.
|
* The ID of the model record that was last inserted.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1537,4 +1537,67 @@ class DboSourceTest extends CakeTestCase {
|
||||||
$this->assertCount(4, $result['Article'][0]['Comment']);
|
$this->assertCount(4, $result['Article'][0]['Comment']);
|
||||||
$this->assertCount(0, $result['Article'][1]['Comment']);
|
$this->assertCount(0, $result['Article'][1]['Comment']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test format of $results in afterFind
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testUseConsistentAfterFind() {
|
||||||
|
$this->loadFixtures('Author', 'Post');
|
||||||
|
|
||||||
|
$expected = array(
|
||||||
|
'Author' => array(
|
||||||
|
'id' => '1',
|
||||||
|
'user' => 'mariano',
|
||||||
|
'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
|
||||||
|
'created' => '2007-03-17 01:16:23',
|
||||||
|
'updated' => '2007-03-17 01:18:31',
|
||||||
|
'test' => 'working',
|
||||||
|
),
|
||||||
|
'Post' => array(
|
||||||
|
array(
|
||||||
|
'id' => '1',
|
||||||
|
'author_id' => '1',
|
||||||
|
'title' => 'First Post',
|
||||||
|
'body' => 'First Post Body',
|
||||||
|
'published' => 'Y',
|
||||||
|
'created' => '2007-03-18 10:39:23',
|
||||||
|
'updated' => '2007-03-18 10:41:31',
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'id' => '3',
|
||||||
|
'author_id' => '1',
|
||||||
|
'title' => 'Third Post',
|
||||||
|
'body' => 'Third Post Body',
|
||||||
|
'published' => 'Y',
|
||||||
|
'created' => '2007-03-18 10:43:23',
|
||||||
|
'updated' => '2007-03-18 10:45:31',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
$Author = new Author();
|
||||||
|
$Post = $this->getMock('Post', array('afterFind'), array(), '', true);
|
||||||
|
$Post->expects($this->at(0))->method('afterFind')->with(array(array('Post' => $expected['Post'][0])), $this->isFalse())->will($this->returnArgument(0));
|
||||||
|
$Post->expects($this->at(1))->method('afterFind')->with(array(array('Post' => $expected['Post'][1])), $this->isFalse())->will($this->returnArgument(0));
|
||||||
|
|
||||||
|
$Author->bindModel(array('hasMany' => array('Post' => array('limit' => 2, 'order' => 'Post.id'))));
|
||||||
|
$Author->Post = $Post;
|
||||||
|
|
||||||
|
$result = $Author->find('first', array('conditions' => array('Author.id' => 1), 'recursive' => 1));
|
||||||
|
$this->assertEquals($expected, $result);
|
||||||
|
|
||||||
|
// Backward compatiblity
|
||||||
|
$Author = new Author();
|
||||||
|
$Post = $this->getMock('Post', array('afterFind'), array(), '', true);
|
||||||
|
$Post->expects($this->once())->method('afterFind')->with($expected['Post'], $this->isFalse())->will($this->returnArgument(0));
|
||||||
|
$Post->useConsistentAfterFind = false;
|
||||||
|
|
||||||
|
$Author->bindModel(array('hasMany' => array('Post' => array('limit' => 2, 'order' => 'Post.id'))));
|
||||||
|
$Author->Post = $Post;
|
||||||
|
|
||||||
|
$result = $Author->find('first', array('conditions' => array('Author.id' => 1), 'recursive' => 1));
|
||||||
|
$this->assertEquals($expected, $result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -732,8 +732,14 @@ class ModifiedAttachment extends CakeTestModel {
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function afterFind($results, $primary = false) {
|
public function afterFind($results, $primary = false) {
|
||||||
if (isset($results['id'])) {
|
if ($this->useConsistentAfterFind) {
|
||||||
$results['callback'] = 'Fired';
|
if (isset($results[0][$this->alias]['id'])) {
|
||||||
|
$results[0][$this->alias]['callback'] = 'Fired';
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (isset($results['id'])) {
|
||||||
|
$results['callback'] = 'Fired';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return $results;
|
return $results;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue