Example #1
0
 /**
  * Creates a new database reference
  *
  * @param string|\Phalcon\Mvc\Collection $collection
  * @param mixed|\MongoId|\Phalcon\Mvc\Collection $id
  * @param string $database
  * @return array
  */
 public static function create($collection, $id = null, $database = null)
 {
     if ($collection instanceof \Phalcon\Mvc\Collection) {
         $id = $collection->getId();
         $collection = $collection->getSource();
     }
     if ($id instanceof \Phalcon\Mvc\Collection) {
         $id = $id->getId();
     }
     if (is_array($collection) && self::isRef($collection)) {
         if (isset($collection['$id'])) {
             $id = $collection['$id'];
         }
         if (isset($collection['$ref'])) {
             $collection = $collection['$ref'];
         }
     }
     if (!$id instanceof \MongoId && $id !== null) {
         $id = new \MongoId($id);
     }
     if ($collection instanceof \MongoId) {
         return $collection;
     }
     if ($id === null) {
         return null;
     }
     return parent::create($collection, $id, $database);
 }
Example #2
0
 /**
  * Creates a new database reference
  *
  * @param string|\Phalcon\Mvc\Collection $collection
  * @param mixed|\MongoId|\Phalcon\Mvc\Collection $id
  * @param string $database
  * @return array
  */
 public static function create($collection, $id = null, $database = null)
 {
     if ($collection instanceof \Phalcon\Mvc\Collection) {
         $id = $collection->getId();
         $collection = $collection->getSource();
     }
     if ($id instanceof \Phalcon\Mvc\Collection) {
         $id = $id->getId();
     }
     if (!$id instanceof \MongoId && $id !== null) {
         $id = new \MongoId($id);
     }
     return parent::create($collection, $id, $database);
 }
Example #3
0
 public function save()
 {
     try {
         return parent::save();
     } catch (\MongoException $e) {
         return false;
     }
 }
Example #4
0
 public function save()
 {
     $result = parent::save();
     if (!$result) {
         throw new \Exception();
     }
     return true;
 }
Example #5
0
    /**
     * The save function stores the assigned fields into
     * the database. Overloaded to...
     * - handle exceptions
     * - convert objects to references before saving
     */
    public function save()
    {
        //convert objects into refs
        $this->deepObjToRef();
        //TODO: Put that in a library class.
        //TODO: use layout
        set_exception_handler(function (\Exception $e) {
            if (ob_get_level() > 0) {
                ob_end_clean();
            }
            echo '<html><head><title>Exception - ', get_class($e), ': ', $e->getMessage(), '</title></head><body>';
            echo '<div class="error-main">
				', get_class($e), ': ', $e->getMessage(), '
				<br/><span class="error-file">', $e->getFile(), ' (', $e->getLine(), ')</span>
				</div>';
            echo '<div class="error-backtrace"><table cellspacing="0">';
            //echo "<pre>"; var_dump($e->getTrace()); echo "</pre>";
            foreach ($e->getTrace() as $n => $trace) {
                echo "<hr>";
                if ($has_line = isset($trace["file"]) && isset($trace["line"])) {
                    echo "<p>" . $trace["file"] . " line " . $trace["line"] . "</p>";
                }
                if ($has_class = isset($trace["class"]) && isset($trace["function"])) {
                    echo "<p>" . $trace["class"] . "::" . $trace["function"] . "</p>";
                }
                if (!$has_line && !$has_class) {
                    echo "<pre>";
                    var_dump($trace);
                    echo "</pre>";
                }
            }
            echo '</table></div>';
            echo '</body></html>';
        });
        $success = parent::save();
        restore_exception_handler();
        //convert refs back into objects
        $this->deepRefToObj();
        return $success;
    }
 /**
  * Allows to query the first record that match the specified conditions
  *
  * @param mixed $parameters
  * @return DataCustomer
  */
 public static function findFirst($parameters = null)
 {
     return parent::findFirst($parameters);
 }
Example #7
0
 public function unserialize($data)
 {
     parent::unserialize($data);
 }
Example #8
0
 /**
  * @return bool|void
  * @throws \Exception
  */
 public function save()
 {
     $this->__operation = 'save';
     $source = $this->getSource();
     if (empty($source)) {
         throw new \Exception("Method getSource() returns empty string");
     }
     $connection = $this->getConnection();
     /**
      * Choose a collection according to the collection name
      */
     $collection = $connection->selectCollection($source);
     /**
      * Check the dirty state of the current operation to update the current operation
      */
     $exists = parent::_exists($collection);
     if ($exists === false) {
         $this->_operationMade = self::OP_CREATE;
     } else {
         $this->_operationMade = self::OP_UPDATE;
     }
     /**
      * The messages added to the validator are reset here
      */
     $this->_errorMessages = [];
     $disableEvents = self::$_disableEvents;
     /**
      * Execute the preSave hook
      */
     if (parent::_preSave($this->getDI(), $disableEvents, $exists) === false) {
         return false;
     }
     $data = $this->toArray();
     $metadata = $this->getMetadata();
     $currentValues = [];
     if (!empty($metadata)) {
         foreach ($this->getObjectProperties() as $object => $value) {
             if (isset($metadata[$object])) {
                 $currentValues[$object] = $this->{$object};
                 if (Scalar::isScalar($metadata[$object])) {
                     $data[$object] = Scalar::map($this->{$object}, $metadata[$object]);
                 } else {
                     $reflectionClass = new \ReflectionClass($metadata[$object]);
                     if ($reflectionClass->isSubclassOf(MapperInterface::class)) {
                         $data[$object] = $reflectionClass->getMethod('createReference')->invoke(null, $this->{$object});
                     }
                 }
             }
         }
     }
     $success = false;
     $status = $collection->save($data, ["w" => true]);
     if (is_array($status)) {
         if (isset($status['ok'])) {
             if ($status['ok']) {
                 $success = true;
                 if ($exists === false) {
                     if (isset($data['_id'])) {
                         $this->_id = $data['_id'];
                     }
                 }
             }
         }
     } else {
         $success = false;
     }
     // clears cache for saved document
     if ($success) {
         $this->clearEntityMappingCache($this->_id);
     }
     $this->__operation = false;
     /**
      * Call the postSave hooks
      */
     return parent::_postSave($disableEvents, $success, $exists);
 }