Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

object graph temporary fixes #123

Open
wants to merge 2 commits into
base: 2.5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions xpdo/om/xpdoobject.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1275,9 +1275,12 @@ public function addOne(& $obj, $alias= '') {
* to this instance via the intersection class.
* @param string $alias An optional alias, required only for instances where
* you have more than one relation defined to the same class.
* @param boolean $graph For object graph use only. If true, the existing object
* with the same value of the primary key will not be overwritten and
* will be used to overwrite the given instance.
* @return boolean Indicates if the addMany was successful.
*/
public function addMany(& $obj, $alias= '') {
public function addMany(& $obj, $alias= '', $graph= false) {
$added= false;
if (!is_array($obj)) {
if (is_object($obj)) {
Expand All @@ -1301,9 +1304,20 @@ public function addMany(& $obj, $alias= '') {
$objpk= implode('-', $objpk);
}
}
$this->_relatedObjects[$alias][$objpk]= $obj;
if ($this->xpdo->getDebug() === true) $this->xpdo->log(xPDO::LOG_LEVEL_DEBUG, 'Added related object with alias: ' . $alias . ' and pk: ' . $objpk . "\n" . print_r($obj->toArray('', true), true));
$added= true;
if ($graph) {
if (isset($this->_relatedObjects[$alias][$objpk])) {
if ($this->xpdo->getDebug() === true) $this->xpdo->log(xPDO::LOG_LEVEL_DEBUG, 'Skipped already existing graph node object with alias: ' . $alias . ' and pk: ' . $objpk . "\n" . print_r($obj->toArray('', true), true));
$obj= $this->_relatedObjects[$alias][$objpk];
} else {
if ($this->xpdo->getDebug() === true) $this->xpdo->log(xPDO::LOG_LEVEL_DEBUG, 'Binded graph node object with alias: ' . $alias . ' and pk: ' . $objpk . "\n" . print_r($obj->toArray('', true), true));
$this->_relatedObjects[$alias][$objpk] = $obj;
$added= true;
}
} else {
$this->_relatedObjects[$alias][$objpk]= $obj;
if ($this->xpdo->getDebug() === true) $this->xpdo->log(xPDO::LOG_LEVEL_DEBUG, 'Added related object with alias: ' . $alias . ' and pk: ' . $objpk . "\n" . print_r($obj->toArray('', true), true));
$added= true;
}
}
}
}
Expand Down
65 changes: 32 additions & 33 deletions xpdo/om/xpdoquery.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,33 +509,31 @@ public function bindGraphNode($parentClass, $parentAlias, $classAlias, $relation
$foreign= $fkMeta['foreign'];
$this->select($this->xpdo->getSelectColumns($class, $classAlias, $classAlias . '_'));
$expression= $this->xpdo->escape($parentAlias) . '.' . $this->xpdo->escape($local) . ' = ' . $this->xpdo->escape($classAlias) . '.' . $this->xpdo->escape($foreign);
if (isset($fkMeta['criteria']['local'])) {
$localCriteria = array();
if (is_array($fkMeta['criteria']['local'])) {
foreach ($fkMeta['criteria']['local'] as $critKey => $critVal) {
if (is_numeric($critKey)) {
$localCriteria[] = $critVal;
} else {
$localCriteria["{$classAlias}.{$critKey}"] = $critVal;
}
$localCriteria = array();
if (isset($fkMeta['criteria']['local']) && is_array($fkMeta['criteria']['local'])) {
foreach ($fkMeta['criteria']['local'] as $critKey => $critVal) {
if (is_numeric($critKey)) {
$localCriteria[] = $critVal;
} else {
$localCriteria["{$parentAlias}.{$critKey}"] = $critVal;
}
}
if (!empty($localCriteria)) {
$expression = array($localCriteria, $expression);
}
$foreignCriteria = array();
if (is_array($fkMeta['criteria']['foreign'])) {
foreach ($fkMeta['criteria']['foreign'] as $critKey => $critVal) {
if (is_numeric($critKey)) {
$foreignCriteria[] = $critVal;
} else {
$foreignCriteria["{$parentAlias}.{$critKey}"] = $critVal;
}
}
if (!empty($localCriteria)) {
$expression = array($localCriteria, $expression);
}
$foreignCriteria = array();
if (isset($fkMeta['criteria']['foreign']) && is_array($fkMeta['criteria']['foreign'])) {
foreach ($fkMeta['criteria']['foreign'] as $critKey => $critVal) {
if (is_numeric($critKey)) {
$foreignCriteria[] = $critVal;
} else {
$foreignCriteria["{$classAlias}.{$critKey}"] = $critVal;
}
}
if (!empty($foreignCriteria)) {
$expression = array($foreignCriteria, $expression);
}
}
if (!empty($foreignCriteria)) {
$expression = array($foreignCriteria, $expression);
}
$this->leftJoin($class, $classAlias, $expression);
if (!empty ($relations)) {
Expand Down Expand Up @@ -609,23 +607,24 @@ public function hydrateGraphParent(& $instances, $row) {
public function hydrateGraphNode(& $row, & $instance, $alias, $relations) {
$relObj= null;
if ($relationMeta= $instance->getFKDefinition($alias)) {
$cardinality = strtolower($relationMeta['cardinality']);
if ($row[$alias.'_'.$relationMeta['foreign']] != null) {
$relObj = $this->xpdo->call($relationMeta['class'], '_loadInstance', array(& $this->xpdo, $relationMeta['class'], $alias, $row));
if ($relObj) {
if (strtolower($relationMeta['cardinality']) == 'many') {
$instance->addMany($relObj, $alias);
} else {
$instance->addOne($relObj, $alias);
}
if ($cardinality === 'many' || empty($instance->_relatedObjects[$alias])) {
$relObj = $this->xpdo->call($relationMeta['class'], '_loadInstance', array(& $this->xpdo, $relationMeta['class'], $alias, $row));
} else {
$relObj = $instance->_relatedObjects[$alias];
}
if ($cardinality === 'many') {
$instance->addMany($relObj, $alias, true);
} else {
$instance->addOne($relObj, $alias);
}
}
}
if (!empty ($relations) && is_object($relObj)) {
while (list($relationAlias, $subRelations)= each($relations)) {
if (is_array($subRelations) && !empty($subRelations)) {
foreach ($subRelations as $subRelation) {
$this->hydrateGraphNode($row, $relObj, $relationAlias, $subRelation);
}
$this->hydrateGraphNode($row, $relObj, $relationAlias, $subRelations);
} else {
$this->hydrateGraphNode($row, $relObj, $relationAlias, null);
}
Expand Down