예제 #1
0
파일: Writer.php 프로젝트: bravo3/orm
 /**
  * Deserialise and hydrate all primitive data in the proxy (not relationships)
  *
  * @return $this
  */
 public function hydrate()
 {
     $serialiser = $this->entity_manager->getSerialiserMap()->getSerialiser($this->serialised_data->getSerialisationCode());
     /** @var OrmProxyInterface $proxy */
     $proxy = $this->getProxy();
     // Deserialise and hydrate the entity
     $serialiser->deserialise($this->metadata, $this->serialised_data, $proxy);
     // Save the original state of all indices so we can compare on consequent persist calls
     $proxy->setOriginalId($this->getReader()->getId());
     foreach ($this->metadata->getIndices() as $index) {
         // If an index is a relationship, automatic hydration will not work due to AOP recursion
         // Instead, check each index column for a relationship and hydrate it manually
         foreach ($index->getColumns() as $column) {
             if ($relationship = $this->metadata->getRelationshipByName($column)) {
                 if (!isset($this->hydrated_methods[$relationship->getGetter()])) {
                     $this->hydrateRelative($relationship);
                 }
             }
         }
         $proxy->setIndexOriginalValue($index->getName(), $this->getReader()->getIndexValue($index));
     }
     $this->is_hydrated = true;
     return $this;
 }
예제 #2
0
 /**
  * Persist some primitive data
  *
  * @param string         $key
  * @param SerialisedData $data
  * @param int            $ttl
  * @return void
  */
 public function persist($key, SerialisedData $data, $ttl = null)
 {
     if (!$ttl) {
         $ttl = 0;
     } else {
         $ttl = time() + $ttl;
     }
     $this->unit_of_work->queueCommand(new Command('write', ['key' => $key, 'payload' => $data->getSerialisationCode() . self::DATA_DELIMITER . $ttl . self::DATA_DELIMITER . $data->getData()]));
 }
예제 #3
0
 /**
  * Deserialise the entity
  *
  * @param Entity         $metadata Metadata object to match the entity
  * @param SerialisedData $data     Data to deserialise
  * @param object         $entity   Entity to hydrate
  */
 public function deserialise(Entity $metadata, SerialisedData $data, $entity)
 {
     // Using $assoc = true is ~ 10-20% quicker on PHP 5.3
     // Source: http://stackoverflow.com/questions/8498114/should-i-use-an-associative-array-or-an-object
     $raw = json_decode($data->getData(), true, self::ENCODE_DEPTH);
     foreach ($metadata->getColumns() as $column) {
         $setter = $column->getSetter();
         $field = $column->getName();
         $value = isset($raw[$field]) ? $raw[$field] : null;
         switch ($column->getType()) {
             default:
                 break;
             case FieldType::DATETIME():
                 $entity->{$setter}($this->deserialiseDateTime($value));
                 break;
             case FieldType::INT():
                 $entity->{$setter}((int) $value);
                 break;
             case FieldType::STRING():
                 $entity->{$setter}((string) $value);
                 break;
             case FieldType::DECIMAL():
                 $entity->{$setter}((double) $value);
                 break;
             case FieldType::BOOL():
                 $entity->{$setter}((bool) $value);
                 break;
             case FieldType::SET():
                 $entity->{$setter}(json_decode($value, true));
                 break;
             case FieldType::OBJECT():
                 $entity->{$setter}($this->deserialiseObject($value, $column->getClassName()));
                 break;
         }
     }
 }
예제 #4
0
 /**
  * Persist some primitive data
  *
  * @param string         $key
  * @param SerialisedData $data
  * @param int            $ttl
  *
  * @return void
  */
 public function persist($key, SerialisedData $data, $ttl = null)
 {
     $params = [$key, $data->getSerialisationCode() . $data->getData()];
     if ($ttl) {
         $params[] = 'EX';
         $params[] = $ttl;
     }
     $this->unit_of_work->addCommand('StringSet', $params);
 }