Joywork/constructor/Models/BaseModel.php
2026-05-22 21:21:54 +03:00

90 lines
2.5 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
abstract class BaseModel {
public $db = null;
public $table_name = null;
public $agent = null;
public $sphinx2 = null;
// public $agent = $this->agent;
public function __construct()
{
global $agent;
$pdo = new MysqlPdo(hst, ndb, user, pass);
$sql = "SET NAMES 'utf8'";
$pdo->query($sql);
$this->db = $pdo;
$this->agent = $agent;
}
public function set_sphinx2(){
$pdo = new MysqlPdo(hstsph2, null, null, null, true, '9306');
$sql = "SET NAMES 'utf8'";
$pdo->query($sql);
$this->sphinx2 = $pdo;
}
public function arrToString($array) {
return implode(',', array_map('intval', $array));
}
public function insert($insertData) {
$fields = [];
$values = [];
foreach ($insertData as $key => $value) {
$fields[] = $key;
// Если ключ properties, то проверяем, что это массив и кодируем его в JSON
if ($key === 'properties' && is_array($value)) {
$values[] = "'" . addslashes(json_encode($value, JSON_UNESCAPED_UNICODE)) . "'";
} else {
$values[] = '"' . addslashes($value) . '"';
}
}
$fields = implode(', ', $fields);
$values = implode(', ', $values);
$sql = "INSERT INTO $this->table_name ($fields)
VALUES ($values)";
return $this->db->query($sql);
}
public function update($updateData, $conditions) {
$setValues = [];
$whereConditions = [];
// Формирование полей для обновления
foreach ($updateData as $key => $value) {
if ($key === 'properties' && is_array($value)) {
$setValues[] = $key . " = '" . addslashes(json_encode($value, JSON_UNESCAPED_UNICODE)) . "'";
} else {
$setValues[] = $key . " = \"" . addslashes($value) . "\"";
}
}
// Формирование условий для WHERE
foreach ($conditions as $field => $value) {
$whereConditions[] = $field . " = \"" . addslashes($value) . "\"";
}
// Объединяем части
$setValues = implode(', ', $setValues);
$whereClause = implode(' AND ', $whereConditions); // Соединяем условия с 'AND'
// Построение запроса
$sql = "UPDATE $this->table_name
SET $setValues
WHERE $whereClause";
return $this->db->query($sql);
}
// public function delete($deleteData) {
// }
public function agent() {
global $agent;
return $this.$agent;
}
}