/**
  * Saves this entity if its a brand new instance that does not exist 
  * in the database; otherwise updates this entity in the database by 
  * flushing all its attributes directly to the database.
  */
 public function saveOrUpdate()
 {
     try {
         // if this entity has been destroyed then we cannot
         // assert the validaty of this instance identity
         // so we simply ignore any persistence operations.
         if ($this->isDestroyed() == true) {
             return;
         }
         // avoid getting fields array directly, in case a
         // sub-class of this object overrides the default
         // behavior for listing this objects fields.
         $fields = $this->getAttributes();
         if ($this->id) {
             // since this is an update operation we
             // update the modification timestamps.
             if (array_key_exists('modified', $fields)) {
                 $this->modified = $this->getDate();
             }
             if (array_key_exists('modifiedid', $fields)) {
                 $this->modifiedid = time();
             }
             $sql = 'UPDATE ' . $this->class . ' SET ';
             // attributes to update
             foreach ($this->fields as $k => $v) {
                 $sql .= $this->assignUpdateSemantics($k, $v);
             }
             // constraint update to this instance identity.
             $sql = rtrim($sql, ',') . ' WHERE id = ' . $this->id;
             DataAccess::getInstance()->query($sql);
         } else {
             // since this is an update operation we
             // update the modification timestamps.
             if (array_key_exists('created', $fields) && $fields['created'] == '') {
                 $this->created = $this->getDate();
             }
             if (array_key_exists('createdid', $fields) && $fields['createdid'] == '') {
                 $this->createdid = time();
             }
             $sql = 'INSERT INTO ' . $this->class . ' (';
             $values = '';
             // values to insert
             foreach ($this->fields as $k => $v) {
                 $sql .= $k . ',';
                 $values .= $this->assignInsertSemantics($v);
             }
             $sql = rtrim($sql, ',') . ') VALUES (' . rtrim($values, ',') . ')';
             DataAccess::getInstance()->query($sql);
             $this->id = DataAccess::getInstance()->lastInsertId();
         }
     } catch (Exception $exception) {
         throw new Exception('Failed to complete save or update.');
     }
 }
Exemple #2
0
 public function __construct()
 {
     $this->dataAccess = DataAccess::getInstance();
 }