Skip to content

Commit

Permalink
Placing the event Model.beforeFind in ORM\Query::sql()
Browse files Browse the repository at this point in the history
This will help getting the correct SQL after all behaviors have
added their changes to the queries even when debugging it or
using it in other queries as an expression.
  • Loading branch information
lorenzo committed Nov 9, 2014
1 parent 5f43bd1 commit 3fa354c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
7 changes: 2 additions & 5 deletions src/Database/Connection.php
Expand Up @@ -256,12 +256,9 @@ public function compileQuery(Query $query, ValueBinder $generator) {
* @return \Cake\Database\StatementInterface executed statement
*/
public function run(Query $query) {
$binder = $query->valueBinder();
$binder->resetCount();
list($query, $sql) = $this->driver()->compileQuery($query, $binder);

$sql = $query->sql();
$statement = $this->prepare($sql);
$binder->attachTo($statement);
$query->valueBinder()->attachTo($statement);
$statement->execute();

return $statement;
Expand Down
22 changes: 14 additions & 8 deletions src/ORM/Query.php
Expand Up @@ -106,6 +106,13 @@ class Query extends DatabaseQuery implements JsonSerializable {
*/
protected $_eagerLoader;

/**
* True if the beforeFind even has already been triggered for this query
*
* @var bool
*/
protected $_beforeFindFired = false;

/**
* Constructor
*
Expand Down Expand Up @@ -601,15 +608,14 @@ public function all() {
*/
public function sql(ValueBinder $binder = null) {
$this->_transformQuery();
return parent::sql($binder);
}

/**
* {@inheritDoc}
*/
public function execute() {
$this->_transformQuery();
return parent::execute();
if (!$this->_beforeFindFired) {
$table = $this->repository();
$table->dispatchEvent('Model.beforeFind', [$this, $this->_options, !$this->eagerLoaded()]);
$this->_beforeFindFired = true;
}

return parent::sql($binder);
}

/**
Expand Down

0 comments on commit 3fa354c

Please sign in to comment.