ORM change alias logic
This commit is contained in:
@@ -943,14 +943,14 @@ abstract class Mapper implements IMapper
|
||||
return $entity;
|
||||
}
|
||||
|
||||
protected function getAlias(IEntity $entity, $key)
|
||||
protected function getAlias(IEntity $entity, $relationName)
|
||||
{
|
||||
if (!isset($this->aliasesCache[$entity->getEntityName()])) {
|
||||
$this->aliasesCache[$entity->getEntityName()] = $this->getTableAliases($entity);
|
||||
}
|
||||
|
||||
if (isset($this->aliasesCache[$entity->getEntityName()][$key])) {
|
||||
return $this->aliasesCache[$entity->getEntityName()][$key];
|
||||
if (isset($this->aliasesCache[$entity->getEntityName()][$relationName])) {
|
||||
return $this->aliasesCache[$entity->getEntityName()][$relationName];
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
@@ -965,21 +965,21 @@ abstract class Mapper implements IMapper
|
||||
|
||||
foreach ($entity->relations as $name => $r) {
|
||||
if ($r['type'] == IEntity::BELONGS_TO) {
|
||||
$key = $r['key'];
|
||||
$table = $this->toDb($r['entity']);
|
||||
|
||||
if (!array_key_exists($key, $aliases)) {
|
||||
if (array_key_exists($table, $occuranceHash)) {
|
||||
$occuranceHash[$table]++;
|
||||
|
||||
if (!array_key_exists($name, $aliases)) {
|
||||
if (array_key_exists($name, $occuranceHash)) {
|
||||
$occuranceHash[$name]++;
|
||||
} else {
|
||||
$occuranceHash[$table] = 0;
|
||||
$occuranceHash[$name] = 0;
|
||||
}
|
||||
$suffix = '_f';
|
||||
if ($occuranceHash[$table] > 0) {
|
||||
$suffix .= '_' . $occuranceHash[$table];
|
||||
$suffix = '';
|
||||
if ($occuranceHash[$name] > 0) {
|
||||
$suffix .= '_' . $occuranceHash[$name];
|
||||
}
|
||||
|
||||
$aliases[$key] = $table . $suffix;
|
||||
$aliases[$name] = $this->toDb($name) . $suffix;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1009,9 +1009,6 @@ abstract class Mapper implements IMapper
|
||||
if (isset($f['relation'])) {
|
||||
$relationName = $f['relation'];
|
||||
|
||||
$keySet = $this->getKeys($entity, $relationName);
|
||||
$key = $keySet['key'];
|
||||
|
||||
$foreigh = $f['foreign'];
|
||||
|
||||
if (is_array($foreigh)) {
|
||||
@@ -1019,12 +1016,12 @@ abstract class Mapper implements IMapper
|
||||
if ($value == ' ') {
|
||||
$foreigh[$i] = '\' \'';
|
||||
} else {
|
||||
$foreigh[$i] = $this->getAlias($entity, $key) . '.' . $this->toDb($value);
|
||||
$foreigh[$i] = $this->getAlias($entity, $relationName) . '.' . $this->toDb($value);
|
||||
}
|
||||
}
|
||||
$fieldPath = 'TRIM(CONCAT(' . implode(', ', $foreigh). '))';
|
||||
} else {
|
||||
$fieldPath = $this->getAlias($entity, $key) . '.' . $this->toDb($foreigh);
|
||||
$fieldPath = $this->getAlias($entity, $relationName) . '.' . $this->toDb($foreigh);
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -1087,10 +1084,8 @@ abstract class Mapper implements IMapper
|
||||
if (isset($fieldDefs['relation'])) {
|
||||
$relationName = $fieldDefs['relation'];
|
||||
if (isset($entity->relations[$relationName])) {
|
||||
$keySet = $this->getKeys($entity, $relationName);
|
||||
$key = $keySet['key'];
|
||||
|
||||
$alias = $this->getAlias($entity, $key);
|
||||
$alias = $this->getAlias($entity, $relationName);
|
||||
if ($alias) {
|
||||
$leftPart = $alias . '.' . $this->toDb($fieldDefs['foreign']);
|
||||
}
|
||||
@@ -1139,7 +1134,7 @@ abstract class Mapper implements IMapper
|
||||
$key = $keySet['key'];
|
||||
$foreignKey = $keySet['foreignKey'];
|
||||
|
||||
$alias = $this->getAlias($entity, $key);
|
||||
$alias = $this->getAlias($entity, $relationName);
|
||||
|
||||
if ($alias) {
|
||||
$joinsArr[] =
|
||||
|
||||
@@ -74,9 +74,9 @@ class DBMapperTest extends PHPUnit_Framework_TestCase
|
||||
public function testSelectById()
|
||||
{
|
||||
$query =
|
||||
"SELECT post.id AS `id`, post.name AS `name`, TRIM(CONCAT(user_f.salutation_name, user_f.first_name, ' ', user_f.last_name)) AS `createdByName`, post.created_by_id AS `createdById`, post.deleted AS `deleted` ".
|
||||
"SELECT post.id AS `id`, post.name AS `name`, TRIM(CONCAT(created_by.salutation_name, created_by.first_name, ' ', created_by.last_name)) AS `createdByName`, post.created_by_id AS `createdById`, post.deleted AS `deleted` ".
|
||||
"FROM `post` ".
|
||||
"LEFT JOIN `user` AS `user_f` ON post.created_by_id = user_f.id " .
|
||||
"LEFT JOIN `user` AS `created_by` ON post.created_by_id = created_by.id " .
|
||||
"WHERE post.id = '1' AND post.deleted = '0'";
|
||||
$return = new MockDBResult(array(
|
||||
array(
|
||||
@@ -94,9 +94,9 @@ class DBMapperTest extends PHPUnit_Framework_TestCase
|
||||
public function testSelect()
|
||||
{
|
||||
$query =
|
||||
"SELECT post.id AS `id`, post.name AS `name`, TRIM(CONCAT(user_f.salutation_name, user_f.first_name, ' ', user_f.last_name)) AS `createdByName`, post.created_by_id AS `createdById`, post.deleted AS `deleted` ".
|
||||
"SELECT post.id AS `id`, post.name AS `name`, TRIM(CONCAT(created_by.salutation_name, created_by.first_name, ' ', created_by.last_name)) AS `createdByName`, post.created_by_id AS `createdById`, post.deleted AS `deleted` ".
|
||||
"FROM `post` ".
|
||||
"LEFT JOIN `user` AS `user_f` ON post.created_by_id = user_f.id " .
|
||||
"LEFT JOIN `user` AS `created_by` ON post.created_by_id = created_by.id " .
|
||||
"JOIN `post_tag` ON post.id = post_tag.post_id AND post_tag.deleted = '0' ".
|
||||
"JOIN `tag` ON tag.id = post_tag.tag_id AND tag.deleted = '0' ".
|
||||
"JOIN `comment` ON post.id = comment.post_id AND comment.deleted = '0' ".
|
||||
@@ -174,9 +174,9 @@ class DBMapperTest extends PHPUnit_Framework_TestCase
|
||||
public function testJoin()
|
||||
{
|
||||
$query =
|
||||
"SELECT comment.id AS `id`, comment.post_id AS `postId`, post_f.name AS `postName`, comment.name AS `name`, comment.deleted AS `deleted` ".
|
||||
"SELECT comment.id AS `id`, comment.post_id AS `postId`, post.name AS `postName`, comment.name AS `name`, comment.deleted AS `deleted` ".
|
||||
"FROM `comment` ".
|
||||
"LEFT JOIN `post` AS `post_f` ON comment.post_id = post_f.id ".
|
||||
"LEFT JOIN `post` AS `post` ON comment.post_id = post.id ".
|
||||
"WHERE comment.deleted = '0'";
|
||||
$return = new MockDBResult(array(
|
||||
array(
|
||||
@@ -244,9 +244,9 @@ class DBMapperTest extends PHPUnit_Framework_TestCase
|
||||
public function testSelectRelatedBelongsTo()
|
||||
{
|
||||
$query =
|
||||
"SELECT post.id AS `id`, post.name AS `name`, TRIM(CONCAT(user_f.salutation_name, user_f.first_name, ' ', user_f.last_name)) AS `createdByName`, post.created_by_id AS `createdById`, post.deleted AS `deleted` ".
|
||||
"SELECT post.id AS `id`, post.name AS `name`, TRIM(CONCAT(created_by.salutation_name, created_by.first_name, ' ', created_by.last_name)) AS `createdByName`, post.created_by_id AS `createdById`, post.deleted AS `deleted` ".
|
||||
"FROM `post` ".
|
||||
"LEFT JOIN `user` AS `user_f` ON post.created_by_id = user_f.id " .
|
||||
"LEFT JOIN `user` AS `created_by` ON post.created_by_id = created_by.id " .
|
||||
"WHERE post.deleted = '0' AND post.id = '1' ".
|
||||
"LIMIT 0, 1";
|
||||
$return = new MockDBResult(array(
|
||||
@@ -394,7 +394,7 @@ class DBMapperTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
public function testMax()
|
||||
{
|
||||
$query = "SELECT MAX(post.id) AS AggregateValue FROM `post` LEFT JOIN `user` AS `user_f` ON post.created_by_id = user_f.id";
|
||||
$query = "SELECT MAX(post.id) AS AggregateValue FROM `post` LEFT JOIN `user` AS `created_by` ON post.created_by_id = created_by.id";
|
||||
$return = new MockDBResult(array(
|
||||
array (
|
||||
'AggregateValue' => 10,
|
||||
|
||||
@@ -68,7 +68,7 @@ class Contact extends TEntity
|
||||
'notStorable' => true,
|
||||
'select' => "TRIM(CONCAT(contact.first_name, ' ', contact.last_name))",
|
||||
'where' => array(
|
||||
'LIKE' => "(contact.first_name LIKE '{text}' OR contact.last_name LIKE '{text}' OR CONCAT(contact.first_name, ' ', contact.last_name) LIKE '{text}')",
|
||||
'LIKE' => "(contact.first_name LIKE {value} OR contact.last_name LIKE {value} OR CONCAT(contact.first_name, ' ', contact.last_name) LIKE {value})",
|
||||
),
|
||||
'orderBy' => "contact.first_name {direction}, contact.last_name {direction}",
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user