Exemple #1
0
 /**
  * (non-PHPdoc)
  * @see Atrox/Core/Data/Atrox_Core_Data_Source#create()
  */
 public function create(Atrox_Core_Data_Entity $entity)
 {
     if (!$entity->beforeCreate($entity)) {
         throw new Exception("beforeCreate failed");
         return false;
     }
     $fields = "";
     $values = "";
     $connection = $this->getConnection();
     $binaries = array();
     foreach ($this->properties as $name => $property) {
         if ($property->hasStorage(Atrox_Core_Data_Property::STORE_CREATE)) {
             $fields .= $connection->parseField("{$name}") . ", ";
             if ($property->getType() == Atrox_Core_Data_Property::TYPE_BINARY) {
                 $binary = $entity->get($name);
                 if ($this->hasFileError($binary["File"]["Error"])) {
                     return false;
                 }
                 if ($binary["File"]["Error"] == 0) {
                     $binary["Hash"] = $name . "/" . md5(uniqid(null, true));
                     $value = $binary["Hash"] . "/" . $binary["File"]["Filename"];
                     $binaries[] = $binary;
                 } else {
                     $value = "";
                 }
             } else {
                 $value = $property->formatForSourceInput($entity->get($name));
             }
             if ($value === "" || $value === null) {
                 $values .= "null, ";
             } else {
                 $values .= $connection->parseValue($value) . ", ";
             }
         }
     }
     // Crop the last comma
     $fields = mb_substr($fields, 0, -2);
     $values = mb_substr($values, 0, -2);
     $sql = "INSERT INTO {$this->parsedTableName} ({$fields}) VALUES ({$values});";
     if ($result = $connection->query($sql)) {
         $entity->setId($connection->getLastId($this->parsedSequenceName));
         foreach ($binaries as $binary) {
             $storageProvider = $this->serviceLocator->getStorageAdaptor();
             try {
                 $storageProvider->makeBucket($this->getTableName() . "/" . $binary["Hash"]);
             } catch (Exception $e) {
             }
             $storageProvider->putBinaryFile($binary["File"]["TempName"], $this->getTableName() . "/" . $binary["Hash"], $binary["File"]["Filename"], $binary["File"]["Type"]);
         }
         if (!$entity->afterCreate($entity)) {
             throw new Exception("afterCreate failed");
             return false;
         }
         return true;
     } else {
         return false;
     }
 }