Exemple #1
0
 /**
  * @param object $obj
  * @param bool   $force
  *
  * @return bool|void
  */
 public function insert(&$obj, $force = false)
 {
     // Make sure object is of correct type
     if (strcasecmp($this->classname, get_class($obj)) != 0) {
         return false;
     }
     // Make sure object needs to be stored in DB
     if (!$obj->isDirty()) {
         return true;
     }
     // Make sure object fields are filled with valid values
     if (!$obj->cleanVars()) {
         return false;
     }
     // Create query for DB update
     if ($obj->isNew()) {
         // Determine next auto-gen ID for table
         $this->_db->genId($this->_db->prefix($this->_dbtable) . '_uid_seq');
         $sql = $this->_insertQuery($obj);
     } else {
         $sql = $this->_updateQuery($obj);
     }
     // Update DB
     if (false != $force) {
         $result = $this->_db->queryF($sql);
     } else {
         $result = $this->_db->query($sql);
     }
     if (!$result) {
         $obj->setErrors('The query returned an error. ' . $this->db->error());
         return false;
     }
     //Make sure auto-gen ID is stored correctly in object
     if ($obj->isNew()) {
         $obj->assignVar($this->_idfield, $this->_db->getInsertId());
     }
     return true;
 }