Browse Source

various fixes

Nathaniel van Diepen 6 years ago
parent
commit
28209bb55e
3 changed files with 29 additions and 11 deletions
  1. 6 2
      Data/securestring.class.php
  2. 22 5
      PDO/table.class.php
  3. 1 4
      orm.abstract.class.php

+ 6 - 2
Data/securestring.class.php

@@ -3,7 +3,7 @@
 	require_once('encryption.class.php');
 
 	class SecureString implements \JsonSerializable {
-		private $data;
+		private $data = '';
 		private $password;
 		private $iv;
 		private $encryption;
@@ -26,7 +26,11 @@
 			}while($data != (string)$this);
 		}
 		public function __toString(){
-			return ''.$this->encryption->decrypt($this->data, $this->password, $this->iv);
+			try{
+				return ''.$this->encryption->decrypt($this->data, $this->password, $this->iv);
+			}catch(\Exception $e){
+				return '';
+			}
 		}
 		public function jsonSerialize(){
 			return "{$this}";

+ 22 - 5
PDO/table.class.php

@@ -397,7 +397,18 @@
 			}
 		}
 		public function insert(array $data){
-			return $this->exists ? $this->pdo->exec("insert into `{$this->name}` {$this->pdo->stringSet($data)}") : 0;
+			if(!$this->exists){
+				return 0;
+			}
+			$id = 0;
+			$name = $this->name;
+			$this->pdo->beginTransaction();
+			$query = $this->pdo->query("insert into `{$name}` {$this->pdo->stringSet($data)}");
+			$query->execute();
+			$id = $this->lastInsertId();
+			$query->closeCursor();
+			$this->pdo->commit();
+			return $id;
 		}
 		public function update(array $data, array $filter = null){
 			return $this->exists ? $this->pdo->exec("update `{$this->name}` {$this->pdo->stringSet($data)} {$this->pdo->stringFilter($filter)}") : 0;
@@ -433,15 +444,21 @@
 			return $this;
 		}
 		public function count(array $filter = null){
-			return $this->exists ? $this->pdo->exec("select 1 from `{$this->name}` {$this->pdo->stringFilter($filter)}") : 0;
+			if(!$this->exists){
+				return 0;
+			}
+			$query = $this->pdo->query("select count(1) from `{$this->name}` {$this->pdo->stringFilter($filter)}");
+			$count = $query->fetchColumn();
+			$query->closeCursor();
+			return $count;
 		}
 		public function lastInsertId(){
 			if(!$this->exists){
 				return 0;
 			}
 			$id = $this->pdo->lastInsertId();
-			if($id === 0){
-				$col;
+			if($id == 0){
+				$col = '';
 				foreach($this->primaryKey as $key => $name){
 					$column = $this->column($name);
 					if($column['increment']){
@@ -449,7 +466,7 @@
 						break;
 					}
 				}
-				if(count($filter) > 0){
+				if(count($col) > 0){
 					$query = $this->pdo->query("select {$col} from `{$this->name}` where {$col} = (select max({$col}) from `{$this->name}`)");
 					$id = $query->fetchColumn();
 				}

+ 1 - 4
orm.abstract.class.php

@@ -324,10 +324,7 @@
 						trigger_error("Save of {$this->name}#{$this->id} may have failed. No affected rows.", E_USER_WARNING);
 					}
 				}else{
-					if($table->insert($data) === 0){
-						trigger_error("First save of {$this->name} instance may have failed. No affected rows.", E_USER_WARNING);
-					}
-					$this->_data[$pk] = $table->lastInsertId();
+					$this->_data[$pk] = $table->insert($data);
 					if($this->_data[$pk] === 0){
 						trigger_error("First save of {$this->name} instance may have failed. PK is 0", E_USER_WARNING);
 					}