Notice: Undefined index: set in C:\xampp\htdocs\DaizerCMS\engine\Core\Database\QueryBuilder.php on line 94
QueryBuilder.php
<?php
namespace Engine\Core\Database;
class QueryBuilder
{
/**
* @var array
*/
protected $sql = [];
/**
* @var array
*/
public $values = [];
/**
* @param string $fields
* @return $this
*/
public function select($fields = '*')
{
$this->reset();
$this->sql['select'] = "SELECT {$fields} ";
return $this;
}
/**
* @return $this
*/
public function delete()
{
$this->reset();
$this->sql['delete'] = "DELETE ";
return $this;
}
/**
* @param $table
* @return $this
*/
public function from($table)
{
$this->sql['from'] = "FROM {$table} ";
return $this;
}
/**
* @param string $column
* @param string $value
* @param string $operator
* @return $this
*/
public function where($column, $value, $operator = '=')
{
$this->sql['where'][] = "{$column} {$operator} ?";
$this->values[] = $value;
return $this;
}
/**
* @param $field
* @param $order
* @return $this
*/
public function orderBy($field, $order)
{
$this->sql['order_by'] = "ORDER BY {$field} {$order}";
return $this;
}
/**
* @param $number
* @return $this
*/
public function limit($number)
{
$this->sql['limit'] = " LIMIT {$number}";
return $this;
}
/**
* @param $table
* @return $this
*/
public function update($table)
{
$this->reset();
$this->sql['update'] = "UPDATE {$table} ";
return $this;
}
public function insert($table)
{
$this->reset();
$this->sql['insert'] = "INSERT INTO {$table} ";
return $this;
}
/**
* @param array $data
* @return $this
*/
public function set($data = [])
{
$this->sql['set'] .= "SET ";
if(!empty($data)) {
foreach ($data as $key => $value) {
$this->sql['set'] .= "{$key} = ?";
if (next($data)) {
$this->sql['set'] .= ", ";
}
$this->values[] = $value;
}
}
return $this;
}
/**
* @return string
*/
public function sql()
{
$sql = '';
if(!empty($this->sql)) {
foreach ($this->sql as $key => $value) {
if ($key == 'where') {
$sql .= ' WHERE ';
foreach ($value as $where) {
$sql .= $where;
if (count($value) > 1 and next($value)) {
$sql .= ' AND ';
}
}
} else {
$sql .= $value;
}
}
}
return $sql;
}
/**
* Reset Builder
*/
public function reset()
{
$this->sql = [];
$this->values = [];
}
}
User.php
<?php
namespace Admin\Model\User;
use Engine\Core\Database\ActiveRecord;
class User
{
use ActiveRecord;
/**
* @var string
*/
public $table = 'user';
/**
* @var User id
*/
public $id;
/**
* @var User email
*/
public $email;
/**
* @var User password
*/
public $password;
/**
* @var User role
*/
public $role;
/**
* @var User hash
*/
public $hash;
/**
* @var User date_reg
*/
public $date_reg;
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @param $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* @param User $email
*/
public function setEmail($email)
{
$this->email = $email;
}
/**
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* @param User $password
*/
public function setPassword($password)
{
$this->password = $password;
}
/**
* @return string
*/
public function getRole()
{
return $this->role;
}
/**
* @param User $role
*/
public function setRole($role)
{
$this->role = $role;
}
/**
* @return string
*/
public function getHash()
{
return $this->hash;
}
/**
* @param User $hash
*/
public function setHash($hash)
{
$this->hash = $hash;
}
/**
* @return mixed
*/
public function getDateReg()
{
return $this->date_reg;
}
/**
* @param mixed $date_reg
*/
public function setDateReg($date_reg)
{
$this->date_reg = $date_reg;
}
}
ActiveRecord.php
<?php
namespace Engine\Core\Database;
use \ReflectionClass;
use \ReflectionProperty;
trait ActiveRecord
{
/**
* @var Connection
*/
protected $db;
/**
* @var QueryBuilder
*/
protected $queryBuilder;
/**
* ActiveRecord constructor.
* @param int $id
*/
public function __construct($id = 0)
{
global $di;
$this->db = $di->get('db');
$this->queryBuilder = new QueryBuilder();
if ($id) {
$this->setId($id);
}
}
/**
* @return string
*/
public function getTable()
{
return $this->table;
}
/**
* Save User
*/
public function save() {
$properties = $this->getIssetProperties();
try {
if (isset($this->id)) {
$this->db->execute(
$this->queryBuilder->update($this->getTable())
->set($properties)
->where('id', $this->id)
->sql(),
$this->queryBuilder->values
);
} else {
$this->db->execute(
$this->queryBuilder->insert($this->getTable())
->set($properties)
->sql(),
$this->queryBuilder->values
);
}
} catch (\Exception $e) {
echo $e->getMessage();
}
}
/**
* @return array
*/
private function getIssetProperties()
{
$properties = [];
foreach ($this->getProperties() as $key => $property) {
if (isset($this->{$property->getName()})) {
$properties[$property->getName()] = $this->{$property->getName()};
}
}
return $properties;
}
/**
* @return ReflectionProperty[]
*/
private function getProperties()
{
$reflection = new ReflectionClass($this);
$properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
return $properties;
}
}
UserRepository.php
<?php
namespace Admin\Model\User;
use Engine\Model;
class UserRepository extends Model
{
public function getUsers()
{
$sql = $this->queryBuilder->select()
->from('user')
->orderBy('id','DESC')
->sql();
return $this->db->query($sql);
}
public function test()
{
$user = new User(1);
$user->setEmail("admin1@admin.com");
$user->save();
}
}
DashboardController.php
<?php
namespace Admin\Controller;
class DashboardController extends AdminController
{
public function index(){
$userModel = $this->load->model('User');
$userModel->repository->test();
print_r($userModel->repository->getUsers());
$this->view->render('dashboard');
}
}
Строка 94 в QueryBuilder.php
$this->sql['set'] .= "SET ";
Если закоментить строку $userModel->repository->test(); в DashboardController.php
ошибка пропадает.
Кофе для программистов: как напиток влияет на продуктивность кодеров?
Рекламные вывески: как привлечь внимание и увеличить продажи
Стратегії та тренди в SMM - Технології, що формують майбутнє сьогодні
Выделенный сервер, что это, для чего нужен и какие характеристики важны?
Современные решения для бизнеса: как облачные и виртуальные технологии меняют рынок
Возникла ошибка после обновления Opencart c 22 До 2
Как передать в SMARTY через $smarty->assign(); Данную функцию и использовать её дальше :
Устанавливаю Куки через phpИспользую данную запись: setCookie('usname', $_POST['name'], 2553454800, '/'); При этом в отладчике Chrome они отображаются как Session
php код генерит адрес потока с защищенным ключом и отдаёт плееру ссылку, при таком методе плеер пропадает, есть конфликт php с js, подскажите как...