|
@@ -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();
|
|
|
}
|