Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
nateabele committed Dec 28, 2012
2 parents fcf34d8 + 7f6da57 commit 505c158
Show file tree
Hide file tree
Showing 10 changed files with 280 additions and 184 deletions.
38 changes: 0 additions & 38 deletions README.md

This file was deleted.

4 changes: 4 additions & 0 deletions analysis/Parser.php
Expand Up @@ -74,6 +74,10 @@ public static function tokenize($code, array $options = array()) {
} }
} }
$tokens[] = array('id' => $id, 'name' => $name, 'content' => $content, 'line' => $line); $tokens[] = array('id' => $id, 'name' => $name, 'content' => $content, 'line' => $line);

if ($id === T_WHITESPACE) {
$line += count(preg_split('/\r\n|\r|\n/', $content)) - 1;
}
} }


if ($options['wrap'] && empty($options['include'])) { if ($options['wrap'] && empty($options['include'])) {
Expand Down
65 changes: 15 additions & 50 deletions data/model/Query.php
Expand Up @@ -11,6 +11,7 @@
use lithium\util\Set; use lithium\util\Set;
use lithium\data\Source; use lithium\data\Source;
use lithium\core\ConfigException; use lithium\core\ConfigException;
use InvalidArgumentException;


/** /**
* The `Query` class acts as a container for all information necessary to perform a particular * The `Query` class acts as a container for all information necessary to perform a particular
Expand Down Expand Up @@ -106,15 +107,6 @@ class Query extends \lithium\core\Object {
*/ */
protected $_paths = array(); protected $_paths = array();


/**
* Map beetween relation paths and their corresponding fieldname paths
*
* @see lithium\data\model\Query::alias()
*
* @var array
*/
protected $_relationNames = array();

/** /**
* Map beetween generated aliases and corresponding models. * Map beetween generated aliases and corresponding models.
* *
Expand Down Expand Up @@ -518,11 +510,15 @@ public function data($data = array()) {
* @param array $config the config array to set. * @param array $config the config array to set.
* @return mixed The relationships array or a relationship array if `$relpath` is set. Returns * @return mixed The relationships array or a relationship array if `$relpath` is set. Returns
* `null` if a join doesn't exist. * `null` if a join doesn't exist.
* @throws InvalidArgumentException
*/ */
public function relationships($relpath = null, $config = null) { public function relationships($relpath = null, $config = null) {
if ($config) { if ($config) {
if (!$relpath) { if (!$relpath) {
throw new ConfigException("The relation dotted path is empty."); throw new InvalidArgumentException("The relation dotted path is empty.");
}
if (isset($config['model']) && isset($config['alias'])) {
$this->_models[$config['alias']] = $config['model'];
} }
$this->_config['relationships'][$relpath] = $config; $this->_config['relationships'][$relpath] = $config;
return $this; return $this;
Expand Down Expand Up @@ -699,26 +695,20 @@ public function alias($alias = true, $relpath = null) {
return $return ?: null; return $return ?: null;
} }


if ($relpath) { if ($relpath === null) {
$oldAlias = array_search($relpath, $this->_paths); $this->_config['alias'] = $alias;
} else {
$oldAlias = array_search('', $this->_paths);
} }
unset($this->_models[$oldAlias]);
unset($this->_paths[$oldAlias]);

$model = $this->_config['model'];


if (!$relpath) { if ($relpath === null && ($model = $this->_config['model'])) {
$this->_alias[$alias] = 1;
$this->_models[$alias] = $model; $this->_models[$alias] = $model;
$this->_paths[$alias] = '';
return $this->_config['alias'] = $alias;
} }


$paths = explode('.', $relpath); $relpath = (string) $relpath;
if (!$alias) { unset($this->_paths[array_search($relpath, $this->_paths)]);
$alias = end($paths);
if (!$alias && $relpath) {
$last = strrpos($relpath, '.');
$alias = $last ? substr($relpath, $last + 1) : $relpath;
} }


if (isset($this->_alias[$alias])) { if (isset($this->_alias[$alias])) {
Expand All @@ -729,34 +719,9 @@ public function alias($alias = true, $relpath = null) {
} }


$this->_paths[$alias] = $relpath; $this->_paths[$alias] = $relpath;
$fieldname = array();
foreach ($paths as $path) {
if (!$relation = $model::relations($path)) {
$model = null;
break;
}
$fieldname[] = $relation->fieldName();
$model = $relation->to();
}
$this->_models[$alias] = $model;
$this->_relationNames[$relpath] = join('.', $fieldname);
return $alias; return $alias;
} }


/**
* Return the relation paths mapped to their corredponding fieldname paths.
*
* @param object $source Instance of the data source (`lithium\data\Source`) to use for
* conversion.
* @return array Map between relation paths and their corresponding fieldname paths.
*/
public function relationNames(Source $source = null) {
if ($source) {
$this->applyStrategy($source);
}
return $this->_relationNames;
}

/** /**
* Return the generated aliases mapped to their relation path * Return the generated aliases mapped to their relation path
* *
Expand Down
8 changes: 3 additions & 5 deletions data/source/Database.php
Expand Up @@ -213,8 +213,7 @@ public function __construct(array $config = array()) {
'type' => $rel->type(), 'type' => $rel->type(),
'model' => $rel->to(), 'model' => $rel->to(),
'fieldName' => $rel->fieldName(), 'fieldName' => $rel->fieldName(),
'fromAlias' => $from, 'alias' => $to
'toAlias' => $to
)); ));
$self->join($context, $rel, $from, $to, $constraints); $self->join($context, $rel, $from, $to, $constraints);
} }
Expand Down Expand Up @@ -473,11 +472,10 @@ public function read($query, array $options = array()) {
case 'array': case 'array':
$columns = $args['schema'] ?: $self->schema($query, $result); $columns = $args['schema'] ?: $self->schema($query, $result);


if (!isset($columns['']) || !is_array($columns[''])) { if (!is_array(reset($columns))) {
$columns = array('' => $columns); $columns = array('' => $columns);
} }


$relationNames = is_object($query) ? $query->relationNames($self) : array();
$i = 0; $i = 0;
$records = array(); $records = array();
foreach ($result as $data) { foreach ($result as $data) {
Expand All @@ -487,7 +485,7 @@ public function read($query, array $options = array()) {
$len = count($cols); $len = count($cols);
$values = array_combine($cols, array_slice($data, $offset, $len)); $values = array_combine($cols, array_slice($data, $offset, $len));
if ($path) { if ($path) {
$records[$i][$relationNames[$path]] = $values; $records[$i][$path] = $values;
} else { } else {
$records[$i] += $values; $records[$i] += $values;
} }
Expand Down

0 comments on commit 505c158

Please sign in to comment.