mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Avoid unsetting Model's $findMethods when defining a custom find method in AppModel, also add tests for custom finds
This commit is contained in:
parent
acccdcde1f
commit
08a4d0bf63
3 changed files with 92 additions and 0 deletions
|
@ -699,6 +699,8 @@ class Model extends Object implements CakeEventListener {
|
||||||
}
|
}
|
||||||
$this->_mergeVars($merge, 'AppModel');
|
$this->_mergeVars($merge, 'AppModel');
|
||||||
}
|
}
|
||||||
|
$this->_mergeVars(array('findMethods'), 'Model');
|
||||||
|
|
||||||
$this->Behaviors = new BehaviorCollection();
|
$this->Behaviors = new BehaviorCollection();
|
||||||
|
|
||||||
if ($this->useTable !== false) {
|
if ($this->useTable !== false) {
|
||||||
|
|
|
@ -7826,4 +7826,24 @@ class ModelReadTest extends BaseModelTest {
|
||||||
|
|
||||||
$this->assertTrue(is_array($result) && !empty($result));
|
$this->assertTrue(is_array($result) && !empty($result));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test custom find method
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testfindCustom() {
|
||||||
|
$this->loadFixtures('Article');
|
||||||
|
$Article = new CustomArticle();
|
||||||
|
$data = array('user_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
|
||||||
|
$Article->create($data);
|
||||||
|
$Article->save();
|
||||||
|
$this->assertEquals(4, $Article->id);
|
||||||
|
|
||||||
|
$result = $Article->find('published');
|
||||||
|
$this->assertEquals(3, count($result));
|
||||||
|
|
||||||
|
$result = $Article->find('unPublished');
|
||||||
|
$this->assertEquals(1, count($result));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,41 @@
|
||||||
* @since CakePHP(tm) v 1.2.0.6464
|
* @since CakePHP(tm) v 1.2.0.6464
|
||||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||||
*/
|
*/
|
||||||
|
App::uses('Model', 'Model');
|
||||||
|
/**
|
||||||
|
* AppModel class
|
||||||
|
*
|
||||||
|
* @package Cake.Test.Case.Model
|
||||||
|
*/
|
||||||
|
class AppModel extends Model {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* findMethods property
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
public $findMethods = array('published' => true);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* useDbConfig property
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
public $useDbConfig = 'test';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* _findPublished custom find
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function _findPublished($state, $query, $results = array()) {
|
||||||
|
if ($state === 'before') {
|
||||||
|
$query['conditions']['published'] = 'Y';
|
||||||
|
return $query;
|
||||||
|
}
|
||||||
|
return $results;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test class
|
* Test class
|
||||||
|
@ -4689,3 +4724,38 @@ class ArmorsPlayer extends CakeTestModel {
|
||||||
public $useDbConfig = 'test_database_three';
|
public $useDbConfig = 'test_database_three';
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CustomArticle class
|
||||||
|
*
|
||||||
|
* @package Cake.Test.Case.Model
|
||||||
|
*/
|
||||||
|
class CustomArticle extends AppModel {
|
||||||
|
/**
|
||||||
|
* useTable property
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $useTable = 'articles';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* findMethods property
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
public $findMethods = array('unPublished' => true);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* _findUnPublished custom find
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function _findUnPublished($state, $query, $results = array()) {
|
||||||
|
if ($state === 'before') {
|
||||||
|
$query['conditions']['published'] = 'N';
|
||||||
|
return $query;
|
||||||
|
}
|
||||||
|
return $results;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue