Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add `data\model\Query::relationNames`.

  • Loading branch information...
commit a0c902a25bd3d1311cca128a4679c8e10ab95888 1 parent 16969a8
@jails jails authored
Showing with 51 additions and 4 deletions.
  1. +30 −4 data/model/Query.php
  2. +21 −0 tests/cases/data/model/QueryTest.php
View
34 data/model/Query.php
@@ -98,7 +98,7 @@ class Query extends \lithium\core\Object {
protected $_alias = array();
/**
- * Map the generated aliases to their corresponding relation path
+ * Map beetween generated aliases and corresponding relation paths
*
* @see lithium\data\model\Query::alias()
*
@@ -107,7 +107,16 @@ class Query extends \lithium\core\Object {
protected $_paths = array();
/**
- * Map the generated aliases to their corresponding model
+ * 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()
*
@@ -720,23 +729,40 @@ 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
* conversion.
- * @return array Map between alias and their corresponding dotted relation
+ * @return array Map between aliases and their corresponding dotted relation paths.
*/
public function paths(Source $source = null) {
if ($source) {
@@ -750,7 +776,7 @@ public function paths(Source $source = null) {
*
* @param object $source Instance of the data source (`lithium\data\Source`) to use for
* conversion.
- * @return array Map between alias and their corresponding model
+ * @return array Map between aliases and their corresponding fully-namespaced model names.
*/
public function models(Source $source = null) {
if ($source) {
View
21 tests/cases/data/model/QueryTest.php
@@ -633,6 +633,27 @@ 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',
Please sign in to comment.
Something went wrong with that request. Please try again.