예제 #1
0
 protected function onExecute(\UniMapper\Connection $connection)
 {
     $adapter = $connection->getAdapter($this->entityReflection->getAdapterName());
     $primaryProperty = $this->entityReflection->getPrimaryProperty();
     $query = $adapter->createSelectOne($this->entityReflection->getAdapterResource(), $primaryProperty->getName(true), $connection->getMapper()->unmapValue($primaryProperty, $this->primaryValue));
     if ($this->associations["local"]) {
         $query->setAssociations($this->associations["local"]);
     }
     $result = $adapter->execute($query);
     if (!$result) {
         return false;
     }
     // Get remote associations
     if ($this->associations["remote"]) {
         settype($result, "array");
         foreach ($this->associations["remote"] as $colName => $association) {
             $assocValue = $result[$association->getKey()];
             $associated = $association->load($connection, [$assocValue]);
             // Merge returned associations
             if (isset($associated[$assocValue])) {
                 $result[$colName] = $associated[$assocValue];
             }
         }
     }
     return $connection->getMapper()->mapEntity($this->entityReflection->getName(), $result);
 }
예제 #2
0
 protected function onExecute(\UniMapper\Connection $connection)
 {
     $adapter = $connection->getAdapter($this->entityReflection->getAdapterName());
     $primaryProperty = $this->entityReflection->getPrimaryProperty();
     $query = $adapter->createDeleteOne($this->entityReflection->getAdapterResource(), $primaryProperty->getName(true), $connection->getMapper()->unmapValue($primaryProperty, $this->primaryValue));
     return (bool) $adapter->execute($query);
 }
예제 #3
0
파일: Count.php 프로젝트: bauer01/unimapper
 protected function onExecute(\UniMapper\Connection $connection)
 {
     $adapter = $connection->getAdapter($this->entityReflection->getAdapterName());
     $query = $adapter->createCount($this->entityReflection->getAdapterResource());
     if ($this->filter) {
         $query->setFilter($connection->getMapper()->unmapFilter($this->entityReflection, $this->filter));
     }
     return (int) $adapter->execute($query);
 }
예제 #4
0
 protected function onExecute(\UniMapper\Connection $connection)
 {
     $adapter = $connection->getAdapter($this->entityReflection->getAdapterName());
     $mapper = $connection->getMapper();
     $query = $adapter->createInsert($this->entityReflection->getAdapterResource(), $mapper->unmapEntity($this->entity), $this->entityReflection->hasPrimary() ? $this->entityReflection->getPrimaryProperty()->getName(true) : null);
     $primaryValue = $adapter->execute($query);
     if ($this->entityReflection->hasPrimary()) {
         $t = $mapper->mapValue($this->entityReflection->getPrimaryProperty(), $primaryValue);
         return $t;
     }
 }
예제 #5
0
 protected function onExecute(\UniMapper\Connection $connection)
 {
     $adapter = $connection->getAdapter($this->entityReflection->getAdapterName());
     $mapper = $connection->getMapper();
     $values = $mapper->unmapEntity($this->entity);
     // Values can not be empty
     if (empty($values)) {
         throw new Exception\QueryException("Nothing to update!");
     }
     $query = $adapter->createUpdateOne($this->entityReflection->getAdapterResource(), $this->entityReflection->getPrimaryProperty()->getName(true), $mapper->unmapValue($this->entityReflection->getPrimaryProperty(), $this->primaryValue), $values);
     return (bool) $adapter->execute($query);
 }
예제 #6
0
 public function load(Connection $connection, array $primaryValues)
 {
     $targetAdapter = $connection->getAdapter($this->targetReflection->getAdapterName());
     $query = $targetAdapter->createSelect($this->getTargetResource());
     $filter = $this->filter;
     $filter[$this->getTargetPrimaryKey()][Entity\Filter::EQUAL] = $primaryValues;
     $query->setFilter($filter);
     $result = $targetAdapter->execute($query);
     if (empty($result)) {
         return [];
     }
     return $this->groupResult($result, [$this->getTargetPrimaryKey()]);
 }
예제 #7
0
 protected function onExecute(\UniMapper\Connection $connection)
 {
     $mapper = $connection->getMapper();
     $values = $mapper->unmapEntity($this->entity);
     // Values can not be empty
     if (empty($values)) {
         throw new Exception\QueryException("Nothing to update!");
     }
     $adapter = $connection->getAdapter($this->entityReflection->getAdapterName());
     $query = $adapter->createUpdate($this->entityReflection->getAdapterResource(), $values);
     if ($this->filter) {
         $query->setFilter($mapper->unmapFilter($this->entityReflection, $this->filter));
     }
     return (int) $adapter->execute($query);
 }
예제 #8
0
 public function saveChanges($primaryValue, Connection $connection, Entity $entity)
 {
     $reflection = Entity\Reflection::load($entity);
     if (!$reflection->hasPrimary()) {
         throw new Exception\InvalidArgumentException("Only entity with primary can save changes!");
     }
     $sourceAdapter = $connection->getAdapter($this->sourceReflection->getAdapterName());
     $primaryName = $reflection->getPrimaryProperty()->getName();
     switch ($entity->getChangeType()) {
         case Entity::CHANGE_ATTACH:
             $adapterQuery = $sourceAdapter->createUpdateOne($this->getSourceResource(), $this->getPrimaryKey(), $primaryValue, [$this->getReferencingKey() => $entity->{$primaryName}]);
             $sourceAdapter->execute($adapterQuery);
             break;
         default:
             break;
     }
 }
예제 #9
0
 public function load(Connection $connection, array $primaryValues)
 {
     $targetAdapter = $connection->getAdapter($this->targetReflection->getAdapterName());
     $query = $targetAdapter->createSelect($this->getTargetResource(), [], $this->orderBy, $this->limit, $this->offset);
     // Set target conditions
     $filter = $this->filter;
     $filter[$this->getReferencedKey()][Entity\Filter::EQUAL] = array_values($primaryValues);
     $query->setFilter($filter);
     $result = $targetAdapter->execute($query);
     if (!$result) {
         return [];
     }
     $return = [];
     foreach ($result as $row) {
         $return[$row[$this->getReferencedKey()]][] = $row;
     }
     return $return;
 }
예제 #10
0
 /**
  * Get adapter
  *
  * @param string $name
  *
  * @return \UniMapper\Adapter
  */
 protected function getAdapter($name)
 {
     return $this->connection->getAdapters()[$name];
 }
예제 #11
0
 protected function onExecute(\UniMapper\Connection $connection)
 {
     $adapter = $connection->getAdapter($this->entityReflection->getAdapterName());
     $mapper = $connection->getMapper();
     $cache = null;
     if ($this->cached) {
         $cache = $connection->getCache();
     }
     if ($cache) {
         $cachedResult = $cache->load($this->_getQueryChecksum());
         if ($cachedResult) {
             return $mapper->mapCollection($this->entityReflection->getName(), $cachedResult);
         }
     }
     $query = $adapter->createSelect($this->entityReflection->getAdapterResource(), $this->createSelection(), $this->orderBy, $this->limit, $this->offset);
     if ($this->filter) {
         $query->setFilter($mapper->unmapFilter($this->entityReflection, $this->filter));
     }
     if ($this->associations["local"]) {
         $query->setAssociations($this->associations["local"]);
     }
     // Execute adapter query
     $result = $adapter->execute($query);
     // Get remote associations
     if ($this->associations["remote"] && !empty($result)) {
         settype($result, "array");
         foreach ($this->associations["remote"] as $colName => $association) {
             $assocKey = $association->getKey();
             $assocValues = [];
             foreach ($result as $item) {
                 if (is_array($item)) {
                     $assocValues[] = $item[$assocKey];
                 } else {
                     $assocValues[] = $item->{$assocKey};
                 }
             }
             $associated = $association->load($connection, $assocValues);
             // Merge returned associations
             if (!empty($associated)) {
                 $result = $this->_mergeAssociated($result, $associated, $assocKey, $colName);
             }
         }
     }
     if ($cache) {
         $cachedOptions = $this->cachedOptions;
         // Add default cache tag
         if (isset($cachedOptions[ICache::TAGS])) {
             $cachedOptions[ICache::TAGS][] = ICache::TAG_QUERY;
             // @todo is it really array?
         } else {
             $cachedOptions[ICache::TAGS] = [ICache::TAG_QUERY];
         }
         // Cache invalidation should depend on entity changes
         if (isset($cachedOptions[ICache::FILES])) {
             $cachedOptions[ICache::FILES] += $this->entityReflection->getRelatedFiles();
         } else {
             $cachedOptions[ICache::FILES] = $this->entityReflection->getRelatedFiles();
         }
         $cache->save($this->_getQueryChecksum(), $result, $cachedOptions);
     }
     return $mapper->mapCollection($this->entityReflection->getName(), empty($result) ? [] : $result);
 }
예제 #12
0
 /**
  * Save changes in target collection
  *
  * @param string            $primaryValue Primary value from source entity
  * @param Connection        $connection
  * @param Entity\Collection $collection   Target collection
  */
 public function saveChanges($primaryValue, Connection $connection, Entity\Collection $collection)
 {
     $sourceAdapter = $connection->getAdapter($this->sourceReflection->getAdapterName());
     $targetAdapter = $connection->getAdapter($this->targetReflection->getAdapterName());
     if ($this->isRemote() && !$this->isDominant()) {
         $sourceAdapter = $targetAdapter;
     }
     $this->_save($primaryValue, $sourceAdapter, $targetAdapter, $collection);
     $this->_save($primaryValue, $sourceAdapter, $targetAdapter, $collection, Adapter\IAdapter::ASSOC_REMOVE);
 }