Skip to content

Commit e5a5af6

Browse files
committed
Separation of relations requests for partitioned tables
1 parent 0a0731c commit e5a5af6

File tree

2 files changed

+66
-24
lines changed

2 files changed

+66
-24
lines changed

framework/db/ar/CActiveFinder.php

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -443,39 +443,72 @@ public function find($criteria=null)
443443
elseif(!$this->_joined && !empty($this->_parent->records)) // not joined before
444444
{
445445
if (empty($this->relation->through) && count((array)$this->relation->foreignKey) === count((array)$this->_parent->_pkAlias)) {
446-
$query = new CJoinQuery($this);
447-
$this->_joined = true;
448-
$query->selects = array(); // reset to not receive the extra keys
449-
$query->selects[] = $this->getColumnSelect($this->relation->select);
450-
$query->selects[] = $this->getRelationsKeys();
451-
$this->buildQuery($query);
452-
$query->conditions[] = $this->relation->on;
453-
$query->conditions[] = $this->buildConditions();
454-
$query->orders[] = $this->relation->order;
446+
if ($this->model->tableSpaceKey()) {
447+
$this->queryWithGroupKey($this->model->tableSpaceKey());
448+
} else {
449+
$query = new CJoinQuery($this);
450+
$this->_joined = true;
451+
$query->selects = []; // reset to not receive the extra keys
452+
$query->selects[] = $this->getColumnSelect($this->relation->select);
453+
$query->selects[] = $this->getRelationsKeys();
454+
$this->buildQuery($query);
455+
$query->conditions[] = $this->relation->on;
456+
$query->conditions[] = $this->buildConditions();
457+
$query->orders[] = $this->relation->order;
458+
$this->_parent->runQuery($query);
459+
}
455460
} else {
456461
$query=new CJoinQuery($this->_parent);
457462
$this->_joined=true;
458463
$query->join($this);
459464
$this->buildQuery($query);
460-
}
461-
$this->_parent->runQuery($query);
465+
$this->_parent->runQuery($query);
466+
}
462467
}
463468

464-
foreach($this->children as $child) // find recursively
465-
$child->find();
469+
foreach ($this->children as $child) // find recursively
470+
$child->find();
466471

467-
foreach($this->stats as $stat)
468-
$stat->query();
469-
}
472+
foreach ($this->stats as $stat)
473+
$stat->query();
474+
}
475+
476+
private function queryWithGroupKey($groupKey)
477+
{
478+
if (is_array($this->_parent->_table->primaryKey)) {
479+
$values = array_keys($this->_parent->records);
480+
$groupingValues = [];
481+
foreach ($values as $value) {
482+
$value = unserialize($value);
483+
$groupingValues[$value[$groupKey]][] = $value;
484+
}
485+
}
486+
487+
$query = new CJoinQuery($this);
488+
$this->_joined = true;
489+
$query->selects = []; // reset to not receive the extra keys
490+
$query->selects[] = $this->getColumnSelect($this->relation->select);
491+
$query->selects[] = $this->getRelationsKeys();
492+
$this->buildQuery($query);
493+
$query->orders[] = $this->relation->order;
494+
495+
foreach ($groupingValues as $groupingValue) {
496+
$query->conditions = [];
497+
$query->conditions[] = $this->relation->on;
498+
$query->conditions[] = $this->buildConditions($groupingValue);
499+
$this->_parent->runQuery($query);
500+
}
501+
}
470502

471-
public function buildConditions()
503+
private function buildConditions($values = null)
472504
{
473-
$values=array_keys($this->_parent->records);
474-
if(is_array($this->_parent->_table->primaryKey))
475-
{
476-
foreach($values as &$value)
477-
$value=unserialize($value);
478-
unset($value);
505+
if (empty($values)) {
506+
$values = array_keys($this->_parent->records);
507+
if (is_array($this->_parent->_table->primaryKey)) {
508+
foreach ($values as &$value)
509+
$value = unserialize($value);
510+
unset($value);
511+
}
479512
}
480513
if (is_array($this->_parent->_table->primaryKey)) {
481514
$keys = array();
@@ -493,7 +526,7 @@ public function buildConditions()
493526
return $this->_builder->createInCondition($this->_table,$keys,$values,$this->getColumnPrefix());
494527
}
495528

496-
public function getRelationsKeys()
529+
private function getRelationsKeys()
497530
{
498531
$fields = array();
499532
$foreignKey = $this->relation->foreignKey;

framework/db/ar/CActiveRecord.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,15 @@ public function primaryKey()
458458
{
459459
}
460460

461+
/**
462+
* Ключ табличного пространтсва
463+
* Если задан, то по нему будет происходить частичная выборка relation'ов
464+
* @return string
465+
*/
466+
public function tableSpaceKey()
467+
{
468+
}
469+
461470
/**
462471
* This method should be overridden to declare related objects.
463472
*

0 commit comments

Comments
 (0)