Paradox/toolbox/PodManager.php
public function store(AModel $model)
    {
        $driver = $this->_toolbox->getDriver();
        $pod = $model->getPod();

        //Signal here
        $this->notify("before_store", $pod);

        $id = null;

        try {
            switch ($pod) {

                   case $pod instanceof Vertex:
                       if ($pod->isNew()) {

                           if ($this->hasTransaction()) {
                               $id = $this->determinePreviouslyStored($model);
                               $this->_toolbox->getTransactionManager()->addWriteCollection($this->_toolbox->getVertexCollectionName());

                               //In a transaction, since all commands are executed in a block on commit, the pod will not have a saved state if it has a previous
                               //store command. This determines if that is the case and rewrites the command as a replace.
                               if ($id !== false) {
                                       return $this->addTransactionCommand("db.{$this->_toolbox->getVertexCollectionName()}.replace(result.$id._id, {$pod->toTransactionJSON()});", "PodManager:store", $model, true);
                               } else {
                                       return $this->addTransactionCommand("graph.addVertex(null, {$pod->toTransactionJSON()})._properties;", "PodManager:store", $model, true);
                               }

                           } else {
                               $doc = $pod->toDriverDocument();
                               $driver->saveVertex($this->_toolbox->getGraph(), $doc);
                               $id = $doc->getInternalId();
                           }

                       } else {

                           if ($this->hasTransaction()) {
                               $this->_toolbox->getTransactionManager()->addWriteCollection($this->_toolbox->getVertexCollectionName());

                               return $this->addTransactionCommand("db.{$this->_toolbox->getVertexCollectionName()}.replace('{$pod->getId()}', {$pod->toTransactionJSON()});", "PodManager:store", $model, true);
                           } else {
                               $doc = $pod->toDriverDocument();
                               $driver->replaceVertex($this->_toolbox->getGraph(), $pod->getId(), $doc);
                           }
                    }
                    break;

                   case $pod instanceof Edge:

                       //Check to see if we have existing keys for the _to and _from vertices
                       $fromKey = $pod->getFromId();
                       $toKey = $pod->getToId();
                       $fromKeyIsJSVar = false;
                       $toKeyIsJSVar = false;

                       //If we don't have a key for from
                       if (!$fromKey) {

                           //Get the from vertex
                        $from = $pod->getFrom();

                        //Save the vertices to get a key for them
                           if ($from) {
                               if ($from->hasChanged()) {

                                   if ($this->hasTransaction()) {
                                       $fromKey = $this->store($from);
                                       $fromKeyIsJSVar = true;
                                   } else {
                                       $this->store($from);
                                       $fromKey = $from->getPod()->getId();
                                       $pod->setFrom($from);
                                   }
                               }

                               //If there are no vertices, throw an error
                        } else {
                            throw new PodManagerException("An edge must have a valid 'from' vertex.");
                        }
                    }

                       //If we don't have a key for to
                       if (!$toKey) {

                        //Get the from vertex
                        $to = $pod->getTo();

                        //Save the vertices to get a key for them
                           if ($to) {
                               if ($to->hasChanged()) {

                                   if ($this->hasTransaction()) {
                                       $toKey = $this->store($to);
                                       $toKeyIsJSVar = true;
                                   } else {
                                       $this->store($to);
                                       $toKey = $to->getPod()->getId();
                                       $pod->setTo($to);
                                   }
                               }

                        //If there are no vertices, throw an error
                        } else {
                            throw new PodManagerException("An edge must have a valid 'to' vertex.");
                        }
                       }

                       if ($pod->isNew()) {

                           if ($this->hasTransaction()) {

                               $id = $this->determinePreviouslyStored($model);
                               $this->_toolbox->getTransactionManager()->addWriteCollection($this->_toolbox->getEdgeCollectionName());

                               //In a transaction, since all commands are executed in a block on commit, the pod will not have a saved state if it has a previous
                               //store command. This determines if that is the case and rewrites the command as a replace.
                               if ($id !== false) {
                                       $this->addTransactionCommand("function () {graph.removeEdge(graph.getEdge(result.$id._id)); return true;}();", "PodManager:store", $model, true);
                                       $this->addTransactionCommand($this->generateCreateEdgeCommand($fromKeyIsJSVar, $fromKey, $toKeyIsJSVar, $toKey, $pod->toTransactionJSON(), "result.$id._key", true, $pod->getLabel()), "PodManager:store", $model, true);
                               } else {
                                       $this->addTransactionCommand($this->generateCreateEdgeCommand($fromKeyIsJSVar, $fromKey, $toKeyIsJSVar, $toKey, $pod->toTransactionJSON(), null, false, $pod->getLabel()), "PodManager:store", $model, true);
                               }
                           } else {
                               $doc = $pod->toDriverDocument();

                               $driver->saveEdge($this->_toolbox->getGraph(), $fromKey, $toKey, null, $doc);
                               $id = $doc->getInternalId();
                           }

                       } else {
                        //We delete the pod then save it, because ArangoDB does not provide a way to update the To and From vertices.
                        if ($this->hasTransaction()) {
                            $this->_toolbox->getTransactionManager()->addWriteCollection($this->_toolbox->getEdgeCollectionName());
                            $this->addTransactionCommand("function () {graph.removeEdge(graph.getEdge('{$pod->getId()}')); return true;}();", "PodManager:store", $model, true);
                            $this->addTransactionCommand($this->generateCreateEdgeCommand($fromKeyIsJSVar, $fromKey, $toKeyIsJSVar, $toKey, $pod->toTransactionJSON(), $pod->getKey(), false, $pod->getLabel()), "PodManager:store", $model, true);
                        } else {
                            $id = $pod->getId();
                            $revision = $pod->getRevision() + mt_rand(0, 1000);
                            $this->delete($model);

                            //Add the id and revision back, because deleting removes it
                            $pod->setId($id);
                            $pod->setRevision($revision);

                            $doc = $pod->toDriverDocument();
                            $driver->saveEdge($this->_toolbox->getGraph(), $fromKey, $toKey, null, $doc);
                        }
                    }
                    break;

                default:
                    $doc = $pod->toDriverDocument();

                    if ($pod->isNew()) {

                        if ($this->hasTransaction()) {
                            $id = $this->determinePreviouslyStored($model);
                            $this->_toolbox->getTransactionManager()->addWriteCollection($pod->getType());

                            //In a transaction, since all commands are executed in a block on commit, the pod will not have a saved state if it has a previous
                            //store command. This determines if that is the case and rewrites the command as a replace.
                            if ($id !== false) {
                                $this->addTransactionCommand("db.{$pod->getType()}.replace(result.$id._id, {$pod->toTransactionJSON()}, true);", "PodManager:store", $model);
                            } else {
                                $this->addTransactionCommand("db.{$pod->getType()}.save({$pod->toTransactionJSON()});", "PodManager:store", $model);
                            }

                        } else {
                            $driver->save($pod->getType(), $doc);
                            $id = $doc->getInternalId();
                        }

                    } else {

                        if ($this->hasTransaction()) {
                            $this->_toolbox->getTransactionManager()->addWriteCollection($pod->getType());
                            $this->addTransactionCommand("db.{$pod->getType()}.replace('{$pod->getId()}', {$pod->toTransactionJSON()}, true);", "PodManager:store", $model);
                        } else {
                            $driver->replace($doc);
                        }
                    }

            }
        } catch (\Exception $e) {
            $normalised = $this->_toolbox->normaliseDriverExceptions($e);
            throw new PodManagerException($normalised['message'], $normalised['code']);
        }

        if (!$this->hasTransaction()) {
            return $this->processStoreResult($pod, $doc->getRevision(), $id);
        }
    }
public function store(AModel $model)
    {
        $driver = $this->_toolbox->getDriver();
        $pod = $model->getPod();

        //Signal here
        $this->notify("before_store", $pod);

        $id = null;

        try {
            switch ($pod) {

                   case $pod instanceof Vertex:
                       if ($pod->isNew()) {

                           if ($this->hasTransaction()) {
                               $id = $this->determinePreviouslyStored($model);
                               $this->_toolbox->getTransactionManager()->addWriteCollection($this->_toolbox->getVertexCollectionName());

                               //In a transaction, since all commands are executed in a block on commit, the pod will not have a saved state if it has a previous
                               //store command. This determines if that is the case and rewrites the command as a replace.
                               if ($id !== false) {
                                       return $this->addTransactionCommand("db.{$this->_toolbox->getVertexCollectionName()}.replace(result.$id._id, {$pod->toTransactionJSON()});", "PodManager:store", $model, true);
                               } else {
                                       return $this->addTransactionCommand("graph.addVertex(null, {$pod->toTransactionJSON()})._properties;", "PodManager:store", $model, true);
                               }

                           } else {
                               $doc = $pod->toDriverDocument();
                               $driver->saveVertex($this->_toolbox->getGraph(), $doc);
                               $id = $doc->getInternalId();
                           }

                       } else {

                           if ($this->hasTransaction()) {
                               $this->_toolbox->getTransactionManager()->addWriteCollection($this->_toolbox->getVertexCollectionName());

                               return $this->addTransactionCommand("db.{$this->_toolbox->getVertexCollectionName()}.replace('{$pod->getId()}', {$pod->toTransactionJSON()});", "PodManager:store", $model, true);
                           } else {
                               $doc = $pod->toDriverDocument();
                               $driver->replaceVertex($this->_toolbox->getGraph(), $pod->getId(), $doc);
                           }
                    }
                    break;

                   case $pod instanceof Edge:

                       //Check to see if we have existing keys for the _to and _from vertices
                       $fromKey = $pod->getFromId();
                       $toKey = $pod->getToId();
                       $fromKeyIsJSVar = false;
                       $toKeyIsJSVar = false;

                       //If we don't have a key for from
                       if (!$fromKey) {

                           //Get the from vertex
                        $from = $pod->getFrom();

                        //Save the vertices to get a key for them
                           if ($from) {
                               if ($from->hasChanged()) {

                                   if ($this->hasTransaction()) {
                                       $fromKey = $this->store($from);
                                       $fromKeyIsJSVar = true;
                                   } else {
                                       $this->store($from);
                                       $fromKey = $from->getPod()->getId();
                                       $pod->setFrom($from);
                                   }
                               }

                               //If there are no vertices, throw an error
                        } else {
                            throw new PodManagerException("An edge must have a valid 'from' vertex.");
                        }
                    }

                       //If we don't have a key for to
                       if (!$toKey) {

                        //Get the from vertex
                        $to = $pod->getTo();

                        //Save the vertices to get a key for them
                           if ($to) {
                               if ($to->hasChanged()) {

                                   if ($this->hasTransaction()) {
                                       $toKey = $this->store($to);
                                       $toKeyIsJSVar = true;
                                   } else {
                                       $this->store($to);
                                       $toKey = $to->getPod()->getId();
                                       $pod->setTo($to);
                                   }
                               }

                        //If there are no vertices, throw an error
                        } else {
                            throw new PodManagerException("An edge must have a valid 'to' vertex.");
                        }
                       }

                       if ($pod->isNew()) {

                           if ($this->hasTransaction()) {

                               $id = $this->determinePreviouslyStored($model);
                               $this->_toolbox->getTransactionManager()->addWriteCollection($this->_toolbox->getEdgeCollectionName());

                               //In a transaction, since all commands are executed in a block on commit, the pod will not have a saved state if it has a previous
                               //store command. This determines if that is the case and rewrites the command as a replace.
                               if ($id !== false) {
                                       $this->addTransactionCommand("function () {graph.removeEdge(graph.getEdge(result.$id._id)); return true;}();", "PodManager:store", $model, true);
                                       $this->addTransactionCommand($this->generateCreateEdgeCommand($fromKeyIsJSVar, $fromKey, $toKeyIsJSVar, $toKey, $pod->toTransactionJSON(), "result.$id._key", true, $pod->getLabel()), "PodManager:store", $model, true);
                               } else {
                                       $this->addTransactionCommand($this->generateCreateEdgeCommand($fromKeyIsJSVar, $fromKey, $toKeyIsJSVar, $toKey, $pod->toTransactionJSON(), null, false, $pod->getLabel()), "PodManager:store", $model, true);
                               }
                           } else {
                               $doc = $pod->toDriverDocument();

                               $driver->saveEdge($this->_toolbox->getGraph(), $fromKey, $toKey, null, $doc);
                               $id = $doc->getInternalId();
                           }

                       } else {
                        //We delete the pod then save it, because ArangoDB does not provide a way to update the To and From vertices.
                        if ($this->hasTransaction()) {
                            $this->_toolbox->getTransactionManager()->addWriteCollection($this->_toolbox->getEdgeCollectionName());
                            $this->addTransactionCommand("function () {graph.removeEdge(graph.getEdge('{$pod->getId()}')); return true;}();", "PodManager:store", $model, true);
                            $this->addTransactionCommand($this->generateCreateEdgeCommand($fromKeyIsJSVar, $fromKey, $toKeyIsJSVar, $toKey, $pod->toTransactionJSON(), $pod->getKey(), false, $pod->getLabel()), "PodManager:store", $model, true);
                        } else {
                            $id = $pod->getId();
                            $revision = $pod->getRevision() + mt_rand(0, 1000);
                            $this->delete($model);

                            //Add the id and revision back, because deleting removes it
                            $pod->setId($id);
                            $pod->setRevision($revision);

                            $doc = $pod->toDriverDocument();
                            $driver->saveEdge($this->_toolbox->getGraph(), $fromKey, $toKey, null, $doc);
                        }
                    }
                    break;

                default:
                    $doc = $pod->toDriverDocument();

                    if ($pod->isNew()) {

                        if ($this->hasTransaction()) {
                            $id = $this->determinePreviouslyStored($model);
                            $this->_toolbox->getTransactionManager()->addWriteCollection($pod->getType());

                            //In a transaction, since all commands are executed in a block on commit, the pod will not have a saved state if it has a previous
                            //store command. This determines if that is the case and rewrites the command as a replace.
                            if ($id !== false) {
                                $this->addTransactionCommand("db.{$pod->getType()}.replace(result.$id._id, {$pod->toTransactionJSON()}, true);", "PodManager:store", $model);
                            } else {
                                $this->addTransactionCommand("db.{$pod->getType()}.save({$pod->toTransactionJSON()});", "PodManager:store", $model);
                            }

                        } else {
                            $driver->save($pod->getType(), $doc);
                            $id = $doc->getInternalId();
                        }

                    } else {

                        if ($this->hasTransaction()) {
                            $this->_toolbox->getTransactionManager()->addWriteCollection($pod->getType());
                            $this->addTransactionCommand("db.{$pod->getType()}.replace('{$pod->getId()}', {$pod->toTransactionJSON()}, true);", "PodManager:store", $model);
                        } else {
                            $driver->replace($doc);
                        }
                    }

            }
        } catch (\Exception $e) {
            $normalised = $this->_toolbox->normaliseDriverExceptions($e);
            throw new PodManagerException($normalised['message'], $normalised['code']);
        }

        if (!$this->hasTransaction()) {
            return $this->processStoreResult($pod, $doc->getRevision(), $id);
        }
    }
Persists a model to the server.
@param  AModel $model
@return int    The id of the saved pod.