Example #1
0
 /**
  * Insert the entity in the database.
  * 
  * @param DBEntity $obj the entity to insert.
  * 
  * @return TRUE if the record has been successfully
  * 		inserted.
  *
  */
 public function insert(&$obj)
 {
     // Signgle record.
     $columns = $obj->getColumns();
     // Create the INSERT clauses
     $columnList = [];
     $values = [];
     $identityPropName = $identityColumnName = null;
     foreach ($columns as $col => $definition) {
         $included = FALSE;
         if (isset($obj->{$col})) {
             if ($definition->isVersion()) {
                 // We do not check the original value expected to
                 // be zero but not necessary if the record is created
                 // from another one.
                 // Nverthesless, we force a value of 1 (version 1 of the record).
                 $obj->{$col} = 1;
             }
             // Only defined properties are included in the
             // INSERT statement
             $val = $obj->{$col};
             if (isset($val)) {
                 // Ignore NULL values
                 $columnList[] = $definition->getName();
                 $values[] = $this->converter->sqlOf($definition, $val);
                 $included = TRUE;
             }
         }
         if (!$included && $definition->isSequence()) {
             $identityPropName = $col;
             $identityColumnName = $definition->getName();
         }
     }
     $table = $obj->getTableName();
     $sql = "INSERT INTO {$table} ( " . implode(",", $columnList) . " ) VALUES ( " . implode(",", $values) . ' );';
     $rows = $this->execute($sql);
     if ($rows < 1) {
         $this->sqlError($sql, "Nothing inserted.");
         return FALSE;
     } else {
         if ($identityPropName) {
             // Get the last inserted ID if apply...
             $obj->{$identityPropName} = $this->getLastId($identityColumnName);
         }
     }
     $obj->setDAO($this);
     return TRUE;
 }