Skip to content
Permalink
Browse files

Always return a Query instance from find()

Even when the beforeFind event prevents the find a Query instance should
be returned. This will allow developers to chain query features without
worrying about callbacks blowing things up.
  • Loading branch information...
markstory committed Aug 5, 2013
1 parent 6d928a4 commit 44622797c7293ab040cc5700105aeead4e9a7bc7
Showing with 12 additions and 5 deletions.
  1. +9 −2 lib/Cake/ORM/Table.php
  2. +3 −3 lib/Cake/Test/TestCase/ORM/TableTest.php
@@ -487,7 +487,14 @@ public function belongsToMany($associated, array $options = []) {
/**
* Creates a new Query for this table and applies some defaults based on the
* type of search that was selected
* type of search that was selected.
*
* ### Model.beforeFind event
*
* Each find() will trigger a `Model.beforeFind` event for all attached
* listeners. Any listener can stop the event to prevent the default
* find handler from being called. When a find() is stopped the Query
* object will still be returned.
*
* @param string $type the type of query to perform
* @param array $options
@@ -500,7 +507,7 @@ public function find($type, $options = []) {
$event = new Event('Model.beforeFind', $this, [$query, $options]);
$this->_eventManager->dispatch($event);
if ($event->isStopped()) {
return $event->result;
return $query;
}
return $this->{'find' . ucfirst($type)}($query, $options);
}
@@ -321,11 +321,11 @@ public function testFindBeforeFindEventOverrideReturn() {
$table->getEventManager()->attach(function ($event, $query, $options) use ($expected) {
$query->setResult($expected);
$event->stopPropagation();
return $query;
}, 'Model.beforeFind');
$result = $table->find('all');
$this->assertEquals($expected, $result->execute());
$query = $table->find('all');
$query->limit(1);
$this->assertEquals($expected, $query->execute());
}
/**

0 comments on commit 4462279

Please sign in to comment.
You can’t perform that action at this time.