Skip to content
Browse files

made into plugin

  • Loading branch information...
1 parent 8170c16 commit c7702f5ba4678eb0f5879d5834427e190f230aed @mcurry committed Apr 30, 2009
Showing with 66 additions and 5 deletions.
  1. +2 −2 README
  2. +3 −3 app_model.php → find_app_model.php
  3. +41 −0 tests/cases/models/find.test.php
  4. +20 −0 tests/fixtures/post_fixture.php
View
4 README
@@ -1,8 +1,8 @@
/*
* App Model custom find types
- * Copyright (c) 2008 Matt Curry
+ * Copyright (c) 2009 Matt Curry
* www.PseudoCoder.com
- * http://github.com/mcurry/cakephp/tree/master/snippets/app_model_find
+ * http://github.com/mcurry/find
* http://www.pseudocoder.com/archives/2008/10/24/cakephp-custom-find-queriescakephp-custom-find-types/
*
* @author Matt Curry <matt@pseudocoder.com>
View
6 app_model.php → find_app_model.php
@@ -11,17 +11,17 @@
*
*/
-class AppModel extends Model {
+class FindAppModel extends Model {
function find($type, $options = array()) {
$method = null;
if(is_string($type)) {
$method = sprintf('__find%s', Inflector::camelize($type));
}
-
if($method && method_exists($this, $method)) {
return $this->{$method}($options);
} else {
- return parent::find($type, $options);
+ $args = func_get_args();
+ return call_user_func_array(array('parent', 'find'), $args);
}
}
}
View
41 tests/cases/models/find.test.php
@@ -0,0 +1,41 @@
+<?php
+App::import('Model', 'Find.app_model');
+
+class Post extends FindAppModel {
+ var $name = 'Post';
+
+ function __findLatest() {
+ return $this->find('first', array('order' => 'created desc'));
+ }
+}
+
+class FindTestCase extends CakeTestCase {
+ var $Post = null;
+ var $fixtures = array('plugin.find.post');
+
+ function start() {
+ parent::start();
+ $this->Post = & ClassRegistry::init('Post');
+ }
+
+ function testPostInstance() {
+ $this->assertTrue(is_a($this->Post, 'Post'));
+ }
+
+ function testFindNative() {
+ $count = $this->Post->find('count');
+ $this->assertEqual(3, $count);
+ }
+
+ function testFindLatest() {
+ $latest = $this->Post->find('latest');
+ $this->assertEqual(array('Post' => array('id' => 3,
+ 'title' => 'Third Post',
+ 'body' =>
+ 'Third Post Body',
+ 'created' => '2009-03-01 12:00:00',
+ 'updated' => '2007-03-18 12:00:00')),
+ $latest);
+ }
+
+}
View
20 tests/fixtures/post_fixture.php
@@ -0,0 +1,20 @@
+<?php
+
+class PostFixture extends CakeTestFixture {
+ var $name = 'Post';
+
+ var $fields = array(
+ 'id' => array('type' => 'integer', 'key' => 'primary'),
+ 'title' => array('type' => 'string', 'null' => false),
+ 'body' => 'text',
+ 'created' => 'datetime',
+ 'updated' => 'datetime'
+ );
+
+ var $records = array(
+ array('id' => 1, 'title' => 'First Post', 'body' => 'First Post Body', 'created' => '2009-01-01 12:00:00', 'updated' => '2007-03-18 12:00:00'),
+ array('id' => 2, 'title' => 'Second Post', 'body' => 'Second Post Body', 'created' => '2009-02-01 12:00:00', 'updated' => '2007-03-18 12:00:00'),
+ array('id' => 3, 'title' => 'Third Post', 'body' => 'Third Post Body', 'created' => '2009-03-01 12:00:00', 'updated' => '2007-03-18 12:00:00')
+ );
+}
+?>

0 comments on commit c7702f5

Please sign in to comment.
Something went wrong with that request. Please try again.