86 lines
2.2 KiB
PHP
86 lines
2.2 KiB
PHP
<?php
|
|
|
|
class IvtCustomer extends mfBaseModel {
|
|
protected $forcestr = ['company','firstname','surname','zip','street','location','housenumber','phone','email','extrainfo'];
|
|
|
|
/**
|
|
* Takes ID or DB row as arguments
|
|
* @param id or table row $_
|
|
*/
|
|
public function __construct($_=NULL) {
|
|
$this->log = mfLoghandler::singleton();
|
|
$this->table = get_class($this);
|
|
$this->data = new stdClass();
|
|
$this->table = "customers";
|
|
|
|
$this->db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);
|
|
|
|
if(is_numeric($_)) {
|
|
$this->fetch($_);
|
|
} elseif(is_object($_)) {
|
|
$this->load($_);
|
|
}
|
|
}
|
|
|
|
public function save() {
|
|
// get AUTO_INCREMENT value
|
|
$safe_ai = $this->getAutoIncrementId();
|
|
if(!$safe_ai) {
|
|
return false;
|
|
}
|
|
$this->log->debug(__CLASS__.": auto increment value: $safe_ai");
|
|
// save customer
|
|
|
|
$fields = [];
|
|
$fields['id'] = $this->id;
|
|
foreach($this->data as $name => $value) {
|
|
$fields[$name] = $value;
|
|
}
|
|
|
|
if(!$this->db->insert($this->table, $fields, $this->forcestr)) {
|
|
return false;
|
|
}
|
|
|
|
// reset AUTO_INCREMENT value
|
|
if($this->setAutoIncrementId($safe_ai)) {
|
|
return true;
|
|
}
|
|
|
|
$this->log->error(__CLASS__.": Unable to reset AUTO_INCREMENT value");
|
|
return false;
|
|
|
|
}
|
|
|
|
private function getAutoIncrementId() {
|
|
$sql = "SELECT `AUTO_INCREMENT` as ai
|
|
FROM INFORMATION_SCHEMA.TABLES
|
|
WHERE TABLE_SCHEMA = 'ivt_xinon'
|
|
AND TABLE_NAME = '".$this->table."'";
|
|
$this->log->debug(__CLASS__.": $sql");
|
|
$res = $this->db->query($sql);
|
|
if($this->db->num_rows($res)) {
|
|
$data = $this->db->fetch_object($res);
|
|
return $data->ai;
|
|
}
|
|
|
|
}
|
|
|
|
private function setAutoIncrementId($ai) {
|
|
$sql = "ALTER TABLE ".$this->table." AUTO_INCREMENT=$ai";
|
|
$this->log->debug(__CLASS__.": $sql");
|
|
|
|
if($this->db->query($sql)) {
|
|
return true;
|
|
}
|
|
return false;
|
|
|
|
}
|
|
|
|
public function __debugInfo() {
|
|
$vars = get_object_vars($this);
|
|
if(is_object($vars['db'])) $vars['db'] = "object(FronkDB)";
|
|
if(is_object($vars['log'])) $vars['log'] = 'object(mfLoghandler)';
|
|
return $vars;
|
|
}
|
|
|
|
} |