6 private $active = FALSE;
7 private $closed = FALSE;
8 private $dbConnections = array();
12 $this->factory = $factory;
15 private function execute(
MDatabase $db, $commands, $transaction = NULL)
17 $this->factory->miolo->ProfileEnter(
'PersistentManager::Execute');
18 if (!is_array($commands))
20 $commands = array($commands);
22 if ($newTransaction = is_null($transaction))
24 $transaction = $db->getTransaction();
26 foreach ($commands as $command)
28 $transaction->addCommand($command);
32 $transaction->process();
34 $this->factory->miolo->ProfileExit(
'PersistentManager::Execute');
39 $classMap = $this->factory->getClassMap($object);
41 $this->_retrieveObject($object, $classMap, $db);
46 $classMap = $this->factory->getClassMap($object);
51 $classMap->retrieveObject($object, $query);
58 $classMap = $this->factory->getClassMap($object);
60 $query = $this->processCriteriaQuery($criteria, $parameters, $db, FALSE);
63 $classMap->retrieveObject($object, $query);
70 $classMap = $this->factory->getClassMap($object);
72 $this->_retrieveObjectAsProxy($object, $classMap, $db);
77 $classMap = $this->factory->getClassMap($object);
84 $classMap = $this->factory->getClassMap($object);
86 $this->_retrieveAssociation($object, $target, $classMap, $db, $distinct);
91 $classMap = $this->factory->getClassMap($object);
93 $this->_retrieveAssociationAsCursor($object, $target, $classMap, $db);
98 $classMap = $this->factory->getClassMap($object);
101 $this->_deleteAssociation($object, $target, $assocObject, $commands, $classMap, $db);
102 $this->execute($db, $commands, $object->getTransaction());
107 $classMap = $this->factory->getClassMap($object);
110 $this->_saveAssociation($object, $target, $commands, $classMap, $db);
111 $this->execute($db, $commands, $object->getTransaction());
116 $classMap = $this->factory->getClassMap($object);
119 $this->_saveObject($object, $classMap, $commands, $db);
120 $this->execute($db, $commands, $object->getTransaction());
125 $classMap = $this->factory->getClassMap($object);
128 $this->_saveObjectRaw($object, $classMap, $commands, $db);
129 $this->execute($db, $commands, $object->getTransaction());
134 $classMap = $this->factory->getClassMap($object);
137 $this->_deleteObject($object, $classMap, $commands, $db);
138 $this->execute($db, $commands, $object->getTransaction());
171 foreach ($associations as $aMap)
173 if ($aMap->isRetrieveAutomatic() && !$aMap->isJoinAutomatic())
175 $this->__retrieveAssociation($object, $aMap, $classMap, $db);
187 if (is_null($aMap->getTarget()))
191 $this->__retrieveAssociation($object, $aMap, $classMap, $db, $distinct);
201 if (is_null($aMap->getTarget()))
205 $orderAttributes = $aMap->getOrderAttributes();
206 $criteria = $aMap->getCriteria($orderAttributes, $this);
207 $criteriaParameters = $aMap->getCriteriaParameters($object);
208 $cursor = $this->processCriteriaCursor($criteria, $criteriaParameters, $db, FALSE);
209 $aMap->getTarget()->setValue($object, $cursor);
219 if (is_null($aMap->getTarget()))
223 $this->__deleteAssociation($object, $aMap, $assocObject, $commands, $classMap, $db);
233 if (is_null($aMap->getTarget()))
237 $this->__saveAssociation($object, $aMap, $commands, $classMap, $db);
243 $criteria = $aMap->
getCriteria($orderAttributes, $this);
244 $criteria->setDistinct($distinct);
246 $cursor = $this->processCriteriaCursor($criteria, $criteriaParameters, $db, FALSE);
250 $value = $cursor->getObject();
252 $target->setValue($object, $value);
258 while ($o = $cursor->getObject())
265 $target->setValue($object, $value);
273 if ($aMap->IsInverse())
275 $classMap = $this->factory->getClassMap($assocObject);
277 for ($i = 0; $i < $aMap->
getSize(); $i++)
279 $aMap->
getEntry($i)->getFrom()->setValue($assocObject,
':NULL');
283 $commands[] = $statement->Update();
288 $target->setValue($object, NULL);
290 for ($i = 0; $i < $aMap->
getSize(); $i++)
292 $aMap->
getEntry($i)->getFrom()->setValue($object,
':NULL');
296 $commands[] = $statement->Update();
302 $associativeObject = $associativeClassMap->getObject();
305 $amA = $associativeClassMap->getAssociationMap($direction[0]);
307 for ($i = 0; $i < $amA->getSize(); $i++)
309 $am = $amA->getEntry($i)->getFrom();
310 $keyValue = $am->getValue($object);
311 $criteria->addCriteria($am,
'=', $keyValue);
314 $amA = $associativeClassMap->getAssociationMap($direction[1]);
316 for ($i = 0; $i < $amA->getSize(); $i++)
318 $am = $amA->getEntry($i)->getFrom();
319 $keyValue = $am->getValue($assocObject);
320 $criteria->addCriteria($am,
'=', $keyValue);
323 $commands[] = $criteria->getSqlStatement()->Delete();
326 $this->__retrieveAssociation($object, $aMap, $classMap, $db);
333 $value = $aMap->
getTarget()->getValue($object);
336 $this->_saveObject($value, $aMap->
getForClass(), $commands, $db);
337 for ($i = 0; $i < $aMap->
getSize(); $i++)
339 $aMap->
getEntry($i)->getFrom()->setValue($object, $aMap->
getEntry($i)->getTo()->getValue($value));
345 $collection = $aMap->
getTarget()->getValue($object);
346 if (count($collection) > 0)
348 foreach ($collection as $value)
350 $this->_saveObject($value, $aMap->
getForClass(), $commands, $db);
351 for ($i = 0; $i < $aMap->
getSize(); $i++)
353 $aMap->
getEntry($i)->getFrom()->setValue($value, $aMap->
getEntry($i)->getTo()->getValue($object));
361 $collection = $aMap->
getTarget()->getValue($object);
362 if (count($collection) > 0)
365 $associativeObject = $associativeClassMap->getObject();
368 $amA = $associativeClassMap->getAssociationMap($direction[0]);
369 for ($i = 0; $i < $amA->getSize(); $i++)
371 $am = $amA->getEntry($i)->getFrom();
372 $keyValue = $am->getValue($object);
373 $criteria->addCriteria($am,
'=', $keyValue);
375 $commands[] = $criteria->getSqlStatement()->Delete();
376 foreach ($collection as $value)
380 $amA = $associativeClassMap->getAssociationMap($direction[0]);
381 for ($i = 0; $i < $amA->getSize(); $i++)
383 $am = $amA->getEntry($i)->getFrom();
384 $am->setValue($associativeObject, $am->getValue($object));
386 $pmA = $associativeClassMap->getAssociationMap($direction[1]);
387 for ($i = 0; $i < $pmA->getSize(); $i++)
389 $pm = $pmA->getEntry($i)->getFrom();
390 $pm->setValue($associativeObject, $pm->getValue($value));
392 $statement = $associativeClassMap->getInsertSqlFor($associativeObject);
393 $commands[] = $statement->Insert();
403 $value = $aMap->
getTarget()->getValue($object);
406 for ($i = 0; $i < $aMap->
getSize(); $i++)
408 $aMap->
getEntry($i)->getFrom()->setValue($value, $aMap->
getEntry($i)->getTo()->getValue($value));
410 $this->_saveObject($value, $aMap->
getForClass(), $commands, $db);
415 $collection = $aMap->
getTarget()->getValue($object);
416 if (count($collection) > 0)
418 foreach ($collection as $value)
420 for ($i = 0; $i < $aMap->
getSize(); $i++)
422 $aMap->
getEntry($i)->getFrom()->setValue($value, $aMap->
getEntry($i)->getTo()->getValue($object));
424 $this->_saveObject($value, $aMap->
getForClass(), $commands, $db);
434 $this->__saveInverseAssociation($object, $aMap, $commands, $classMap, $db);
438 $this->__saveStraightAssociation($object, $aMap, $commands, $classMap, $db);
447 $this->_saveObject($object, $classMap->
getSuperClass(), $commands, $db);
454 $commands[] = $statement->Update();
458 for ($i = 0; $i < $classMap->
getKeySize(); $i++)
462 if ($keyAttribute->getColumnMap()->getKeyType() !=
'primary')
466 if ($keyAttribute->getColumnMap()->getIdGenerator() != NULL)
467 $value = $db->
GetNewId($keyAttribute->getColumnMap()->getIdGenerator());
469 $value = $keyAttribute->getValue($object);
471 $keyAttribute->setValue($object, $value);
476 $commands[] = $statement->Insert();
482 foreach ($associations as $aMap)
484 if ($aMap->isSaveAutomatic())
486 $this->__saveStraightAssociation($object, $aMap, $mmCmd, $classMap, $db);
491 foreach ($associations as $aMap)
493 if ($aMap->isSaveAutomatic())
495 $this->__saveInverseAssociation($object, $aMap, $mmCmd, $classMap, $db);
501 $commands = array_merge($commands, $mmCmd);
512 $commands[] = $statement->Update();
516 for ($i = 0; $i < $classMap->
getKeySize(); $i++)
520 if ($keyAttribute->getColumnMap()->getKeyType() !=
'primary')
524 if ($keyAttribute->getColumnMap()->getIdGenerator() != NULL)
525 $value = $db->
GetNewId($keyAttribute->getColumnMap()->getIdGenerator());
527 $value = $keyAttribute->getValue($object);
529 $keyAttribute->setValue($object, $value);
534 $commands[] = $statement->Insert();
544 foreach ($associations as $aMap)
546 if (!$aMap->isDeleteAutomatic())
549 if ($aMap->getCardinality() ==
'oneToOne')
551 $value = $aMap->getTarget()->getValue($object);
555 $this->_deleteObject($value, $aMap->getForClass(), $commands, $db);
557 for ($i = 0; $i < $aMap->getSize(); $i++)
559 $aMap->getEntry($i)->getFrom()->setValue($object, NULL);
563 elseif ($aMap->getCardinality() ==
'oneToMany')
565 $collection = $aMap->getTarget()->getValue($object);
567 if (count($collection) > 0)
569 foreach ($collection as $value)
571 $this->_deleteObject($value, $aMap->getForClass(), $commands, $db);
573 for ($i = 0; $i < $aMap->getSize(); $i++)
575 $aMap->getEntry($i)->getFrom()->setValue($value, NULL);
580 elseif ($aMap->getCardinality() ==
'manyToMany')
583 $associativeClassMap = $aMap->getAssociativeClass();
584 $associativeObject = $associativeClassMap->getObject();
585 $direction = $aMap->getDirection();
586 $am = $associativeClassMap->getAssociationMap($direction[0])->getEntry(0)->getFrom();
587 $keyValue = $am->getValue($object);
589 $criteria->addCriteria($am,
'=', $keyValue);
590 $mmCmd[] = $criteria->getSqlStatement()->Delete();
596 foreach ($associations as $aMap)
598 if (!$aMap->isDeleteAutomatic())
601 if ($aMap->getCardinality() ==
'oneToOne')
603 $value = $aMap->getTarget()->getValue($object);
607 for ($i = 0; $i < $aMap->getSize(); $i++)
609 $aMap->getEntry($i)->getFrom()->setValue($value, NULL);
612 $this->_deleteObject($value, $aMap->getForClass(), $commands, $db);
615 elseif (($aMap->getCardinality() ==
'oneToMany') || ($aMap->getCardinality() ==
'manyToMany'))
617 $collection = $aMap->getTarget()->getValue($object);
619 if (count($collection) > 0)
621 foreach ($collection as $value)
623 for ($i = 0; $i < $aMap->getSize(); $i++)
625 $aMap->getEntry($i)->getFrom()->setValue($value, NULL);
628 $this->_deleteObject($value, $aMap->getForClass(), $commands, $db);
635 $commands[] = $statement->Delete();
638 $commands = array_merge($mmCmd, $commands);
642 $this->_deleteObject($object, $classMap->superClass, $commands, $db);
650 $statement = $criteria->getSqlStatement($forProxy);
651 $statement->SetParameters($parameters);
658 $query = $this->processCriteriaQuery($criteria, $parameters, $db, $forProxy);
665 $classMap = $this->factory->getClassMap($object);
672 $classMap = $this->factory->getClassMap($object);
674 $criteria->setTransaction($object->getTransaction());
682 $statement->SetParameters($parameters);
683 $this->execute($db, $statement->delete(), $criteria->
getTransaction());
690 $query = $this->processCriteriaQuery($criteria, $parameters, $db, FALSE);
697 $cursor = $this->processCriteriaCursor($criteria, $parameters, $db, FALSE);
704 $query = $this->processCriteriaQuery($criteria, $parameters, $db,
true);
711 $cursor = $this->processCriteriaCursor($criteria, $parameters, $db,
true);
719 throw new EPersistenManagerException(
"Persistent Manager is closed!");
723 if (($conn = $this->dbConnections[$dbName]) == NULL)
725 $conn = $this->factory->miolo->GetDatabase($dbName);
726 $this->dbConnections[$dbName] = $conn;
731 $conn = $this->factory->miolo->GetDatabase($dbName);