Commit a3f2cae8 authored by zYne's avatar zYne

fixes #360

parent d76814c8
...@@ -129,6 +129,8 @@ class Doctrine_RawSql extends Doctrine_Query_Abstract ...@@ -129,6 +129,8 @@ class Doctrine_RawSql extends Doctrine_Query_Abstract
*/ */
public function getQuery() public function getQuery()
{ {
$select = array();
foreach ($this->fields as $field) { foreach ($this->fields as $field) {
$e = explode('.', $field); $e = explode('.', $field);
if ( ! isset($e[1])) { if ( ! isset($e[1])) {
...@@ -143,16 +145,17 @@ class Doctrine_RawSql extends Doctrine_Query_Abstract ...@@ -143,16 +145,17 @@ class Doctrine_RawSql extends Doctrine_Query_Abstract
} }
} }
if ($e[1] == '*') {
$componentAlias = $this->getComponentAlias($e[0]); $componentAlias = $this->getComponentAlias($e[0]);
if ($e[1] == '*') {
foreach ($this->_aliasMap[$componentAlias]['table']->getColumnNames() as $name) { foreach ($this->_aliasMap[$componentAlias]['table']->getColumnNames() as $name) {
$field = $e[0] . '.' . $name; $field = $e[0] . '.' . $name;
$this->parts['select'][$field] = $field . ' AS ' . $e[0] . '__' . $name;
$select[$componentAlias][$field] = $field . ' AS ' . $e[0] . '__' . $name;
} }
} else { } else {
$field = $e[0] . '.' . $e[1]; $field = $e[0] . '.' . $e[1];
$this->parts['select'][$field] = $field . ' AS ' . $e[0] . '__' . $e[1]; $select[$componentAlias][$field] = $field . ' AS ' . $e[0] . '__' . $e[1];
} }
} }
...@@ -165,12 +168,23 @@ class Doctrine_RawSql extends Doctrine_Query_Abstract ...@@ -165,12 +168,23 @@ class Doctrine_RawSql extends Doctrine_Query_Abstract
$field = $tableAlias . '.' . $key; $field = $tableAlias . '.' . $key;
if ( ! isset($this->parts['select'][$field])) { if ( ! isset($this->parts['select'][$field])) {
$this->parts['select'][$field] = $field . ' AS ' . $tableAlias . '__' . $key; $select[$componentAlias][$field] = $field . ' AS ' . $tableAlias . '__' . $key;
} }
} }
} }
$q = 'SELECT ' . implode(', ', $this->parts['select']); // first add the fields of the root component
reset($this->_aliasMap);
$componentAlias = key($this->_aliasMap);
$q = 'SELECT ' . implode(', ', $select[$componentAlias]);
unset($select[$componentAlias]);
foreach ($select as $component => $fields) {
if ( ! empty($fields)) {
$q .= ', ' . implode(', ', $fields);
}
}
$string = $this->applyInheritance(); $string = $this->applyInheritance();
if ( ! empty($string)) { if ( ! empty($string)) {
......
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
*/ */
class Doctrine_RawSql_TestCase extends Doctrine_UnitTestCase class Doctrine_RawSql_TestCase extends Doctrine_UnitTestCase
{ {
public function testQueryParser() public function testQueryParser()
{ {
$sql = 'SELECT {p.*} FROM photos p'; $sql = 'SELECT {p.*} FROM photos p';
...@@ -140,10 +141,10 @@ class Doctrine_RawSql_TestCase extends Doctrine_UnitTestCase ...@@ -140,10 +141,10 @@ class Doctrine_RawSql_TestCase extends Doctrine_UnitTestCase
$count = $this->conn->count(); $count = $this->conn->count();
$coll[4]->Phonenumber[0]->phonenumber; $coll[4]['Phonenumber'][0]['phonenumber'];
$this->assertEqual($count, $this->conn->count()); $this->assertEqual($count, $this->conn->count());
$coll[5]->Phonenumber[0]->phonenumber; $coll[5]['Phonenumber'][0]['phonenumber'];
$this->assertEqual($count, $this->conn->count()); $this->assertEqual($count, $this->conn->count());
} }
public function testPrimaryKeySelectForcing() public function testPrimaryKeySelectForcing()
...@@ -222,16 +223,30 @@ class Doctrine_RawSql_TestCase extends Doctrine_UnitTestCase ...@@ -222,16 +223,30 @@ class Doctrine_RawSql_TestCase extends Doctrine_UnitTestCase
"SELECT entity.name AS entity__name, entity.id AS entity__id FROM (SELECT entity.name FROM entity WHERE entity.name = 'something') WHERE entity.id = 2 ORDER BY entity.name"); "SELECT entity.name AS entity__name, entity.id AS entity__id FROM (SELECT entity.name FROM entity WHERE entity.name = 'something') WHERE entity.id = 2 ORDER BY entity.name");
} }
public function testJoin() public function testSelectingWithoutIdentifiersOnRootComponent()
{ {
$query = new Doctrine_RawSql(); $query = new Doctrine_RawSql();
$query->parseQuery('SELECT {entity.name}, {phonenumber.*} FROM entity LEFT JOIN phonenumber ON phonenumber.entity_id = entity.id LIMIT 1'); $query->parseQuery('SELECT {entity.name}, {phonenumber.*} FROM entity LEFT JOIN phonenumber ON phonenumber.entity_id = entity.id LIMIT 3');
$query->addComponent('entity', 'Entity'); $query->addComponent('entity', 'Entity');
$query->addComponent('phonenumber', 'Entity.Phonenumber'); $query->addComponent('phonenumber', 'Entity.Phonenumber');
$this->assertEqual($query->getSql(), 'SELECT entity.name AS entity__name, entity.id AS entity__id, phonenumber.id AS phonenumber__id, phonenumber.phonenumber AS phonenumber__phonenumber, phonenumber.entity_id AS phonenumber__entity_id FROM entity LEFT JOIN phonenumber ON phonenumber.entity_id = entity.id LIMIT 3');
$coll = $query->execute(array(), Doctrine::FETCH_ARRAY);
$coll = $query->execute(); $this->assertEqual(count($coll), 3);
$this->assertEqual($coll->count(), 1); }
public function testSwitchingTheFieldOrder()
{
$query = new Doctrine_RawSql();
$query->parseQuery('SELECT {phonenumber.*}, {entity.name} FROM entity LEFT JOIN phonenumber ON phonenumber.entity_id = entity.id LIMIT 3');
$query->addComponent('entity', 'Entity');
$query->addComponent('phonenumber', 'Entity.Phonenumber');
$this->assertEqual($query->getSql(), 'SELECT entity.name AS entity__name, entity.id AS entity__id, phonenumber.id AS phonenumber__id, phonenumber.phonenumber AS phonenumber__phonenumber, phonenumber.entity_id AS phonenumber__entity_id FROM entity LEFT JOIN phonenumber ON phonenumber.entity_id = entity.id LIMIT 3');
$coll = $query->execute(array(), Doctrine::FETCH_ARRAY);
$this->assertEqual(count($coll), 3);
} }
} }
?> ?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment