Exemplo n.º 1
0
 protected function _saveAggregatedInstances(&$instance)
 {
     $class_name = get_class($instance);
     $class = new ReflectionClass($class_name);
     $properties = $class->getProperties();
     foreach ($properties as $property) {
         $property_name = $property->getName();
         $getter_method = 'get' . ucfirst(str_replace('_', '', $property_name));
         if (method_exists($instance, $getter_method)) {
             $property_value = $instance->{$getter_method}();
             if ($property_value instanceof IPersistent) {
                 $dao = __DaoManager::getInstance()->getDao($property_value);
                 if ($dao != null) {
                     $dao->save($property_value);
                 } else {
                     //todo, raise or not an exception
                 }
             }
         }
     }
 }
 /**
  * Persist a given collection into the data source
  *
  * @param __Collection|VirtualProxy $collection
  */
 public function save(&$collection)
 {
     if (!$collection instanceof IPersistent) {
         throw __ExceptionFactory::getInstance()->createException('A collection implementing the IPersistent was expected. It was given a ' . get_class($collection) . ' instance.');
     }
     if ($collection instanceof VirtualProxy) {
         if ($collection->isDirty()) {
             $collection = $collection->getReceiver();
         } else {
             return;
         }
     }
     if (!$collection instanceof $this->_collection_class) {
         throw __ExceptionFactory::getInstance()->createException('Can not save instances of type ' . get_class($collection) . ' by ussing ' . get_class($this) . '. A ' . $this->_collection_class . ' instance was expected');
     }
     __DaoManager::getInstance()->beginTransaction($this);
     try {
         $dao = __DaoManager::getInstance()->getDao($this->_class);
         $iterator = $collection->getIterator();
         $iterator->first();
         while (!$iterator->isDone()) {
             $item = $iterator->currentItem();
             $dao->save($item);
             $iterator->next();
         }
         $collection->setDirty(false);
         $collection->setTransient(false);
     } catch (Exception $e) {
         __DaoManager::getInstance()->rollbackTransaction();
         throw $e;
     }
     __DaoManager::getInstance()->commitTransaction();
 }