Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #763 from jails/pr2

Some refactoring of `data\model\Query::alias()`.
  • Loading branch information...
commit 8a8fe88272b9d3c508d4380b815df37bab5e525c 2 parents 204b797 + c189f74
@nateabele nateabele authored
View
65 data/model/Query.php
@@ -11,6 +11,7 @@
use lithium\util\Set;
use lithium\data\Source;
use lithium\core\ConfigException;
+use InvalidArgumentException;
/**
* The `Query` class acts as a container for all information necessary to perform a particular
@@ -107,15 +108,6 @@ class Query extends \lithium\core\Object {
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.
*
* @see lithium\data\model\Query::alias()
@@ -518,11 +510,15 @@ public function data($data = array()) {
* @param array $config the config array to set.
* @return mixed The relationships array or a relationship array if `$relpath` is set. Returns
* `null` if a join doesn't exist.
+ * @throws InvalidArgumentException
*/
public function relationships($relpath = null, $config = null) {
if ($config) {
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;
return $this;
@@ -699,26 +695,20 @@ public function alias($alias = true, $relpath = null) {
return $return ?: null;
}
- if ($relpath) {
- $oldAlias = array_search($relpath, $this->_paths);
- } else {
- $oldAlias = array_search('', $this->_paths);
+ if ($relpath === null) {
+ $this->_config['alias'] = $alias;
}
- unset($this->_models[$oldAlias]);
- unset($this->_paths[$oldAlias]);
-
- $model = $this->_config['model'];
- if (!$relpath) {
- $this->_alias[$alias] = 1;
+ if ($relpath === null && ($model = $this->_config['model'])) {
$this->_models[$alias] = $model;
- $this->_paths[$alias] = '';
- return $this->_config['alias'] = $alias;
}
- $paths = explode('.', $relpath);
- if (!$alias) {
- $alias = end($paths);
+ $relpath = (string) $relpath;
+ unset($this->_paths[array_search($relpath, $this->_paths)]);
+
+ if (!$alias && $relpath) {
+ $last = strrpos($relpath, '.');
+ $alias = $last ? substr($relpath, $last + 1) : $relpath;
}
if (isset($this->_alias[$alias])) {
@@ -729,35 +719,10 @@ public function alias($alias = true, $relpath = null) {
}
$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 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
*
* @param object $source Instance of the data source (`lithium\data\Source`) to use for
View
8 data/source/Database.php
@@ -213,8 +213,7 @@ public function __construct(array $config = array()) {
'type' => $rel->type(),
'model' => $rel->to(),
'fieldName' => $rel->fieldName(),
- 'fromAlias' => $from,
- 'toAlias' => $to
+ 'alias' => $to
));
$self->join($context, $rel, $from, $to, $constraints);
}
@@ -473,11 +472,10 @@ public function read($query, array $options = array()) {
case 'array':
$columns = $args['schema'] ?: $self->schema($query, $result);
- if (!isset($columns['']) || !is_array($columns[''])) {
+ if (!is_array(reset($columns))) {
$columns = array('' => $columns);
}
- $relationNames = is_object($query) ? $query->relationNames($self) : array();
$i = 0;
$records = array();
foreach ($result as $data) {
@@ -487,7 +485,7 @@ public function read($query, array $options = array()) {
$len = count($cols);
$values = array_combine($cols, array_slice($data, $offset, $len));
if ($path) {
- $records[$i][$relationNames[$path]] = $values;
+ $records[$i][$path] = $values;
} else {
$records[$i] += $values;
}
View
59 tests/cases/data/model/QueryTest.php
@@ -38,6 +38,11 @@ public function setUp() {
MockQueryComment::$connection = $this->db;
}
+ public function tearDown() {
+ MockQueryPost::reset();
+ MockQueryComment::reset();
+ }
+
/**
* Tests that configuration settings are delegating to matching method names
*/
@@ -391,9 +396,7 @@ public function testJoins() {
public function testWithAssociation() {
$model = $this->_model;
$model::meta('source', 'foo');
- $model::bind('hasMany', 'MockQueryComment', array(
- 'class' => 'lithium\tests\mocks\data\model\MockQueryComment'
- ));
+ $model::bind('hasMany', 'MockQueryComment');
$query = new Query(compact('model') + array('with' => 'MockQueryComment'));
$export = $query->export(new MockDatabase());
@@ -402,8 +405,7 @@ public function testWithAssociation() {
'type' => 'hasMany',
'model' => 'lithium\tests\mocks\data\model\MockQueryComment',
'fieldName' => 'mock_query_comments',
- 'fromAlias' => 'MockQueryPost',
- 'toAlias' => 'MockQueryComment'
+ 'alias' => 'MockQueryComment'
));
$keyExists = isset($export['relationships']);
$this->assertTrue($keyExists);
@@ -612,8 +614,10 @@ public function testAliasAndPaths() {
public function testModels() {
$model = 'lithium\tests\mocks\data\model\MockQueryPost';
- $query = new Query(compact('model'));
- $query->alias(null, 'MockQueryComment');
+ $query = new Query(array(
+ 'model' => $model,
+ 'with' => 'MockQueryComment'
+ ));
$expected = array(
'MockQueryPost' => 'lithium\tests\mocks\data\model\MockQueryPost',
@@ -621,9 +625,14 @@ public function testModels() {
);
$this->assertEqual($expected, $query->models($this->db));
- $query->alias('Post');
- $query->alias('Comment', 'MockQueryComment');
- $query->alias('Post2', 'MockQueryComment.MockQueryPost');
+ $query = new Query(array(
+ 'model' => $model,
+ 'alias' => 'Post',
+ 'with' => array(
+ 'MockQueryComment' => array('alias' => 'Comment'),
+ 'MockQueryComment.MockQueryPost' => array('alias' => 'Post2'),
+ )
+ ));
$expected = array(
'Post' => 'lithium\tests\mocks\data\model\MockQueryPost',
@@ -633,27 +642,6 @@ public function testModels() {
$this->assertEqual($expected, $query->models($this->db));
}
- public function testRelationNames() {
- $model = 'lithium\tests\mocks\data\model\MockQueryPost';
- $query = new Query(compact('model'));
- $query->alias(null, 'MockQueryComment');
-
- $expected = array(
- 'MockQueryComment' => 'mock_query_comments'
- );
- $this->assertEqual($expected, $query->relationNames($this->db));
-
- $query->alias('Post');
- $query->alias('Comment', 'MockQueryComment');
- $query->alias('Post2', 'MockQueryComment.MockQueryPost');
-
- $expected = array(
- 'MockQueryComment' => 'mock_query_comments',
- 'MockQueryComment.MockQueryPost' => 'mock_query_comments.mock_query_post'
- );
- $this->assertEqual($expected, $query->relationNames($this->db));
- }
-
public function testExportWithJoinedStrategy() {
$query = new Query(array(
'alias' => 'MyAlias',
@@ -702,22 +690,19 @@ public function testExportWithJoinedStrategy() {
'type' => 'hasMany',
'model' => 'lithium\tests\mocks\data\model\MockImage',
'fieldName' => 'images',
- 'fromAlias' => 'MyAlias',
- 'toAlias' => 'Image'
+ 'alias' => 'Image'
),
'Image.ImageTag' => array(
'type' => 'hasMany',
'model' => 'lithium\tests\mocks\data\model\MockImageTag',
'fieldName' => 'image_tags',
- 'fromAlias' => 'Image',
- 'toAlias' => 'ImageTag'
+ 'alias' => 'ImageTag'
),
'Image.ImageTag.Tag' => array(
'type' => 'belongsTo',
'model' => 'lithium\tests\mocks\data\model\MockTag',
'fieldName' => 'tag',
- 'fromAlias' => 'ImageTag',
- 'toAlias' => 'Tag'
+ 'alias' => 'Tag'
)
)
);
View
4 tests/cases/data/source/DatabaseTest.php
@@ -1584,13 +1584,13 @@ public function testReturnArrayOnReadWithQuery() {
'author_id' => '2',
'title' => 'Post title',
'created' => '2012-12-17 17:04:00',
- 'mock_database_comments' => array(
+ 'MockDatabaseComment' => array(
'id' => '3',
'post_id' => '1',
'author_id' => '2',
'body' => 'Very good post',
'created' => '2012-12-17 17:05:00',
- 'mock_database_post' => array(
+ 'MockDatabasePost' => array(
'id' => '1',
'author_id' => '2',
'title' => 'Post title',
Please sign in to comment.
Something went wrong with that request. Please try again.