Browse Source

Be picky setting the primary (originator) query as a Query parameter.

`\Cake\ORM\Query::$_primary = true` only for the primary query, every other
query originated from that one will have the value set to `false`.

`Query::primary()` will return the query `_primary` value.
`Query::primary(true)` will mark the query as being the primary.
`Query::primary(false)` will mark the query as being a byproduct.
Ber Clausen 12 years ago
parent
commit
e97bf6adae

+ 1 - 1
src/Datasource/QueryTrait.php

@@ -185,7 +185,7 @@ trait QueryTrait {
 		}
 
 		$table = $this->repository();
-		$event = new Event('Model.beforeFind', $table, [$this, $this->_options, true]);
+		$event = new Event('Model.beforeFind', $table, [$this, $this->_options, $this->primary()]);
 		$table->getEventManager()->dispatch($event);
 
 		if (isset($this->_results)) {

+ 1 - 0
src/ORM/Association/ExternalAssociationTrait.php

@@ -228,6 +228,7 @@ trait ExternalAssociationTrait {
 		$fetchQuery = $this
 			->find('all')
 			->where($options['conditions'])
+			->primary(false)
 			->hydrate($options['query']->hydrate());
 		$fetchQuery = $this->_addFilteringCondition($fetchQuery, $key, $filter);
 

+ 22 - 0
src/ORM/Query.php

@@ -96,6 +96,13 @@ class Query extends DatabaseQuery {
 	protected $_eagerLoader;
 
 /**
+ * Whether the query is the primary query or a byproduct.
+ *
+ * @var boolean
+ */
+	protected $_primary = true;
+
+/**
  * Constuctor
  *
  * @param \Cake\Database\Connection $connection
@@ -647,6 +654,21 @@ class Query extends DatabaseQuery {
 	}
 
 /**
+ * Sets the query instance to be the primary query. If no argument is passed,
+ * the current configured query `_primary` value is returned.
+ *
+ * @param boolean $value
+ * @return \Cake\ORM\Query
+ */
+	public function primary($value = null) {
+		if ($value === null) {
+			return $this->_primary;
+		}
+		$this->_primary = $value;
+		return $this;
+	}
+
+/**
  * Create an update query.
  *
  * This changes the query type to be 'update'.