Skip to content
Permalink
Browse files

Cleaning up and simplifying code after adding the new finder feature

  • Loading branch information...
lorenzo committed Sep 6, 2014
1 parent f223725 commit 36ba3bb14d3dd40f2cd2fcf2a75429be75c32163
Showing with 36 additions and 44 deletions.
  1. +33 −3 src/ORM/Association.php
  2. +3 −1 src/ORM/Association/SelectableAssociationTrait.php
  3. +0 −40 src/ORM/Table.php
@@ -446,7 +446,7 @@ public function attachTo(Query $query, array $options = []) {
'fields' => [],
'type' => empty($options['matching']) ? $this->joinType() : 'INNER',
'table' => $target->table(),
'finder' => null
'finder' => $this->finder()
];
if (!empty($options['foreignKey'])) {
@@ -456,7 +456,10 @@ public function attachTo(Query $query, array $options = []) {
}
}
$dummy = $this->find($options['finder'])->eagerLoaded(true);
list($finder, $opts) = $this->_extractFinder($options['finder']);
$dummy = $this
->find($finder, $opts)
->eagerLoaded(true);
if (!empty($options['queryBuilder'])) {
$dummy = $options['queryBuilder']($dummy);
if (!($dummy instanceof Query)) {
@@ -531,8 +534,9 @@ public function defaultRowValue($row, $joined) {
*/
public function find($type = null, array $options = []) {
$type = $type ?: $this->finder();
list($type, $opts) = $this->_extractFinder($type, $options);
return $this->target()
->find($type, $options)
->find($type, $options + $opts)
->where($this->conditions());
}
@@ -708,6 +712,32 @@ protected function _joinCondition($options) {
return $conditions;
}
/**
* Helper method to infer the requested finder and its options.
*
* Returns the inferred options from the finder $type.
*
* ### Examples:
*
* The following will call the finder 'translations' with the value of the finder as its options:
* $query->contain(['Comments' => ['finder' => ['translations']]]);
* $query->contain(['Comments' => ['finder' => ['translations' => []]]]);
* $query->contain(['Comments' => ['finder' => ['translations' => ['locales' => ['en_US']]]]]);
*
* @param string|array $finderData The finder name or an array having the name as key
* and options as value.
* @return array
*/
protected function _extractFinder($finderData) {
$finderData = (array)$finderData;
if (is_numeric(key($finderData))) {
return [current($finderData), []];
}
return [key($finderData), current($finderData)];
}

This comment has been minimized.

Copy link
@patrickconroy

patrickconroy Sep 7, 2014

Contributor

current/key ftw 👍 Thanks for putting this in the right places, @lorenzo. It'll really help make things easier.

/**
* Proxies property retrieval to the target table. This is handy for getting this
* association's associations
@@ -85,8 +85,10 @@ protected function _buildQuery($options) {
$filter = $this->_buildSubquery($options['query']);
}
$finder = isset($options['finder']) ? $options['finder'] : $this->finder();
list($finder, $opts) = $this->_extractFinder($finder);
$fetchQuery = $this
->find(isset($options['finder']) ? $options['finder'] : 'all')
->find($finder, $opts)
->where($options['conditions'])
->eagerLoaded(true)
->hydrate($options['query']->hydrate());
@@ -1454,9 +1454,6 @@ protected function _processDelete($entity, $options) {
* @throws \BadMethodCallException
*/
public function callFinder($type, Query $query, array $options = []) {
if (is_array($type)) {
list($type, $options) = $this->_inferFinderTypeAndOptions($type, $options);
}
$query->applyOptions($options);
$options = $query->getOptions();
$finder = 'find' . $type;
@@ -1951,41 +1948,4 @@ public function __debugInfo() {
];
}
/**
* Helper method to infer the requested finder and its options.
*
* Returns the inferred options from the finder $type.
*
* ### Examples:
*
* The following will call the finder 'translations' with the value of the finder as its options:
* $query->contain(['Comments' => ['finder' => ['translations']]]);
* $query->contain(['Comments' => ['finder' => ['translations' => []]]]);
* $query->contain(['Comments' => ['finder' => ['translations' => ['locales' => ['en_US']]]]]);
* $query->contain(['Comments' => ['finder' => ['translations' => 'customOption']]]);
*
* @param array $type Finder type as an array.
* @return array
*/
protected function _inferFinderTypeAndOptions(array $type) {
$options = [];
if (count($type)) {
$v = array_values($type)[0];
$k = array_keys($type)[0];
if (is_array($v)) {
$type = $k;
$options = $v;
} elseif (is_string($v) && !$k) {
$type = $v;
} elseif (is_string($v)) {
$type = $k;
$options = [$v];
}
}
return [
$type,
$options
];
}
}

0 comments on commit 36ba3bb

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