Exemplo n.º 1
0
 public function store(ModelInterface $model, array $options = array())
 {
     // determine metadata and data from model
     $metadata = $this->metadataManager->loadByModel($model);
     $data = $this->serializer->serialize($model, array_merge($metadata->getDataFields(), $metadata->getMetadataFields()));
     // generate hash if necessary
     if ($metadata->isHashGenerated()) {
         $hash = $this->factory->createHash(json_encode($data));
     } elseif (!($hash = $model->getHash())) {
         throw new \Exception('Hash not specified');
     }
     $this->accessor->setValue($model, 'hash', $hash);
     $isNew = !$this->contains($hash, $model->getClass());
     // dispatch event
     $event = new DatabaseStoreEvent($model, $data, $isNew, $metadata, $options);
     $this->eventDispatcher->dispatch(DatabaseEvent::STORE_EVENT, $event);
     // possibility to cancel store in a event-handler
     if ($event->isCanceled()) {
         return;
     }
     // possibility to change model in the event-handler to a stub
     $model = $event->getModel();
     $data = $event->getData();
     // prepare object
     $object = array('data' => $data, 'class' => $model->getClass());
     // append existing policies
     if ($model instanceof DistributedModelInterface) {
         $object['policies'] = serialize($model->getPolicyCollection());
     }
     // store it
     $this->storageAdapter->store($hash, $object, $metadata->getContext());
     $this->searchAdapter->index($hash, $model, $metadata);
     return $model;
 }