1
0
mirror of synced 2024-12-13 14:56:01 +03:00
This commit is contained in:
zYne 2007-06-26 10:05:26 +00:00
parent d76814c8d1
commit a3f2cae863
2 changed files with 41 additions and 12 deletions

View File

@ -129,6 +129,8 @@ class Doctrine_RawSql extends Doctrine_Query_Abstract
*/
public function getQuery()
{
$select = array();
foreach ($this->fields as $field) {
$e = explode('.', $field);
if ( ! isset($e[1])) {
@ -143,16 +145,17 @@ class Doctrine_RawSql extends Doctrine_Query_Abstract
}
}
$componentAlias = $this->getComponentAlias($e[0]);
if ($e[1] == '*') {
$componentAlias = $this->getComponentAlias($e[0]);
foreach ($this->_aliasMap[$componentAlias]['table']->getColumnNames() as $name) {
$field = $e[0] . '.' . $name;
$this->parts['select'][$field] = $field . ' AS ' . $e[0] . '__' . $name;
$select[$componentAlias][$field] = $field . ' AS ' . $e[0] . '__' . $name;
}
} else {
$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
$field = $tableAlias . '.' . $key;
if ( ! isset($this->parts['select'][$field])) {
$this->parts['select'][$field] = $field . ' AS ' . $tableAlias . '__' . $key;
$select[$componentAlias][$field] = $field . ' AS ' . $tableAlias . '__' . $key;
}
}
}
// first add the fields of the root component
reset($this->_aliasMap);
$componentAlias = key($this->_aliasMap);
$q = 'SELECT ' . implode(', ', $this->parts['select']);
$q = 'SELECT ' . implode(', ', $select[$componentAlias]);
unset($select[$componentAlias]);
foreach ($select as $component => $fields) {
if ( ! empty($fields)) {
$q .= ', ' . implode(', ', $fields);
}
}
$string = $this->applyInheritance();
if ( ! empty($string)) {

View File

@ -33,6 +33,7 @@
*/
class Doctrine_RawSql_TestCase extends Doctrine_UnitTestCase
{
public function testQueryParser()
{
$sql = 'SELECT {p.*} FROM photos p';
@ -140,10 +141,10 @@ class Doctrine_RawSql_TestCase extends Doctrine_UnitTestCase
$count = $this->conn->count();
$coll[4]->Phonenumber[0]->phonenumber;
$coll[4]['Phonenumber'][0]['phonenumber'];
$this->assertEqual($count, $this->conn->count());
$coll[5]->Phonenumber[0]->phonenumber;
$coll[5]['Phonenumber'][0]['phonenumber'];
$this->assertEqual($count, $this->conn->count());
}
public function testPrimaryKeySelectForcing()
@ -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");
}
public function testJoin()
public function testSelectingWithoutIdentifiersOnRootComponent()
{
$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('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($coll->count(), 1);
$this->assertEqual(count($coll), 3);
}
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);
}
}
?>