Shanty_Mongo_Document::save PHP Method

save() public method

Save this document
public save ( boolean $entierDocument = false, boolean $safe = true ) : boolean
$entierDocument boolean Force the saving of the entier document, instead of just the changes
$safe boolean If FALSE, the program continues executing without waiting for a database response. If TRUE, the program will wait for the database response and throw a MongoCursorException if the update did not succeed
return boolean Result of save
    public function save($entierDocument = false, $safe = true)
    {
        if (!$this->isConnected()) {
            require_once 'Shanty/Mongo/Exception.php';
            throw new Shanty_Mongo_Exception('Can not save documet. Document is not connected to a db and collection');
        }
        if ($this->isLocked()) {
            require_once 'Shanty/Mongo/Exception.php';
            throw new Shanty_Mongo_Exception('Can not save documet. Document is locked.');
        }
        ## execute pre hooks
        if ($this->isNewDocument()) {
            $this->preInsert();
        } else {
            $this->preUpdate();
        }
        $this->preSave();
        $exportData = $this->export();
        //Remove data with Ignore requirement.
        $this->removeIgnoredProperties($exportData);
        if ($this->isRootDocument() && ($this->isNewDocument() || $entierDocument)) {
            // Save the entier document
            $operations = $exportData;
        } else {
            // Update an existing document and only send the changes
            if (!$this->isRootDocument()) {
                // are we updating a child of an array?
                if ($this->isNewDocument() && $this->isParentDocumentSet()) {
                    $this->_operations['$push'][$this->getPathToDocument()] = $exportData;
                    $exportData = array();
                    /**
                     * We need to lock this document because it has an incomplete document path and there is no way to find out it's true path.
                     * Locking prevents overriding the parent array on another save() after this save().
                     */
                    $this->setConfigAttribute('locked', true);
                }
            }
            // Convert all data changes into sets and unsets
            $this->processChanges($exportData);
            $operations = $this->getOperations(true);
            // There are no changes, return so we don't blank the object
            if (empty($operations)) {
                return true;
            }
        }
        $result = false;
        if ($this->isNewDocument()) {
            $result = $this->_getMongoCollection(true)->update($this->getCriteria(), $operations, array('upsert' => true, 'safe' => $safe));
            $this->_cleanData = $exportData;
        } else {
            $newversion = $this->_getMongoDb(true)->command(array('findandmodify' => $this->getConfigAttribute('collection'), 'query' => $this->getCriteria(), 'update' => $operations, 'new' => true));
            if (isset($newversion['value'])) {
                $this->_cleanData = $newversion['value'];
            }
            if ($newversion['ok'] == 1) {
                $result = true;
            }
        }
        $this->_data = array();
        $this->purgeOperations(true);
        // Run post hooks
        if ($this->isNewDocument()) {
            // This is not a new document anymore
            $this->setConfigAttribute('new', false);
            $this->postInsert();
        } else {
            $this->postUpdate();
        }
        $this->postSave();
        return $result;
    }