예제 #1
0
 /**
  * Obtem o ultimo ID inserido independente do tipo de query
  * @return int
  */
 public static function lastInsertId()
 {
     if (CRUDQuery::isPDOMode()) {
         return self::$conn->lastInsertId();
     } elseif (CRUDQuery::isMySqliMode()) {
         return mysqli_insert_id(CRUDQuery::getConn());
     }
 }
예제 #2
0
 /**
  * Encerra o projeto e a conexão
  * @return void
  */
 public static function close()
 {
     if (CRUDQuery::isPDOMode()) {
         Connection::close();
     } elseif (CRUDQuery::isMySqliMode()) {
         ConnectionMySqli::close();
     }
 }
예제 #3
0
 /**
  * Salva um objeto, caso o ID dele ja exista no banco entao a feito um
  * updateObject caso nao exista a feito um insert
  * @param mixed $objecta
  * @return boolean
  */
 public function save($object = null)
 {
     //	$this->ref = new ReflectionClass($this->getClassName());
     //	try {
     //	    $this->ref = new ReflectionClass($this->getClassName());
     //	} catch ( ReflectionException $ex ) {
     //	    echo "A Classe especificada não existe: " . $ex->getMessage();
     //	}
     if ($object == null) {
         $object = $this->ref->newInstance();
     }
     $refMe = $this->ref->getMethod(MethodSintaxe::buildGetterName($this->getKeyColunm()));
     $id = $refMe->invoke($object);
     if (!self::isEmpty($id)) {
         //caso o id exista ele
         $q1 = "SELECT " . $this->getKeyColunm() . " FROM " . $this->getTableName() . " WHERE " . $this->getKeyColunm() . "=" . $id;
         //mysqli_query(self::getConn() , $q1);
         $r1 = self::executeQuery($q1);
         //            if (strlen(mysqli_error(self::getConn())) > 0) {
         //                throw new CRUDException("SQL ERROR: $q1 - " . mysqli_error(self::getConn()));
         //            }
         $executeUpdate = false;
         if (CRUDQuery::isPDOMode()) {
             if ($r1 && $r1->rowCount() > 0) {
                 $executeUpdate = true;
             } else {
                 $executeUpdate = false;
             }
         } elseif (CRUDQuery::isMySqliMode()) {
             if (mysqli_num_rows($r1) > 0) {
                 $executeUpdate = true;
             } else {
                 $executeUpdate = false;
             }
         }
         if ($executeUpdate) {
             $object = $this->update($object);
             //se ja existir ele salva
             return $object;
         } else {
             $object = $this->insertObject($object);
             // se não existir ele insere
             return $object;
         }
     } else {
         $object = $this->insertObject($object);
         //caso não exista id setado ele insere
         return $object;
     }
 }