<?php
class ServerActiveRecord extends ServerDataGateway {
protected $id;
protected $delid;
protected $user;
protected $pw;
protected $account;
protected $ipaddress;
function __construct($id=0,$delid=0,$user,$pw,
$account,$ipaddress)
{
$this->id = $id;
$this->delid = $delid;
$this->user = $user;
$this->pw = $pw;
$this->account = $account;
$this->ipaddress = $ipaddress;
// set database connection
$dbconn = new ServerDBConn();
$this->db = $dbconn->connectDB();
// check if has data to delete
if(isset($this->delid)) {
$this->hasDelIdentity();
}
}
// check if there is some data to process
public function store() {
if($this->hasIdentity()) {
$this->update();
} else {
$this->insert();
}
}
public function update() {
$stmt = $this->db->prepare("UPDATE tb_restlogin
SET login_id = ?,
user = ?,
pw = ?,
account_id = ?,
ipaddress = ?,
last_visit = ?
WHERE login_id = $this->id;
");
$stmt->bind_param('isssss', $this->id,
$this->setUser(),
$this->setPassword(),
$this->setAccountId(),
$this->setIpAddress(),
$this->setDatum()
);
// execute prepared statement
$stmt->execute();
// close statement and connection
$stmt->close();
// return last inserted id
echo $this->showIdentity($this->id);
}
public function insert() {
$stmt = $this->db->prepare("INSERT INTO tb_restlogin
(login_id, user, pw, account_id,
ipaddress, last_visit)
VALUES (NULL, ?, ?, ?, ?, ?)
");
$stmt->bind_param('sssss', $this->setUser(),
$this->setPassword(),
$this->setAccountId(),
$this->setIpAddress(),
$this->setDatum()
);
// execute prepared statement
$stmt->execute();
// close statement and connection
$stmt->close();
// return last inserted id
echo $this->showIdentity($this->id);
}
public function delete() {
if($stmt = $this->db->prepare("DELETE FROM tb_restlogin
WHERE login_id = ? ")) {
$stmt->bind_param('i', $this->delid);
// execute prepared statement
$stmt->execute();
}
if(!$this->db->affected_rows) {
exit();
}
// close statement and connection
$stmt->close();
// return last deleted id
echo $this->showIdentity($this->delid);
}
public function hasIdentity() {
if($this->id == 0 || $this->id == "") {
return false;
}
return true;
}
public function hasDelIdentity() {
if($this->delid == 0 || $this->delid == "") {
return false;
}
$this->delete();
}
public function showIdentity($id) {
$this->id = $id;
if($this->id == 0 || $this->id == "") {
return $this->db->insert_id; }
else {
return $this->id;
}
}
}
?>