Exemplo n.º 1
0
 /**
  * Retrieve an entity by its identifier.
  *
  * @param  mixed $id
  * @return mixed
  * @throws \Exception
  */
 public function find($id)
 {
     if ($entity = $this->manager->getById($id, $this->metadata->getClass())) {
         return $entity;
     }
     return $this->where([$this->metadata->getIdentifier() => $id])->first();
 }
Exemplo n.º 2
0
 /**
  * Loads an entity or creates a new one if it does not already exist.
  *
  * @param  Metadata $metadata
  * @param  array    $data
  * @return object
  */
 public function load(Metadata $metadata, array $data)
 {
     $id = $data[$metadata->getIdentifier()];
     $class = $metadata->getClass();
     if (isset($this->entities[$class][$id])) {
         $entity = $this->entities[$class][$id];
     } else {
         $entity = $metadata->newInstance();
         $metadata->setValues($entity, $data, true, true);
         $this->identifiers[spl_object_hash($entity)] = $id;
         $this->entities[$class][$id] = $entity;
     }
     $this->manager->dispatchEvent(Events::postLoad, $entity, $metadata);
     return $entity;
 }