Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace EngineCoreDatabase;
- 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 = [];
- }
- }
- <?php
- namespace AdminModelUser;
- use EngineCoreDatabaseActiveRecord;
- 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;
- }
- }
- <?php
- namespace EngineCoreDatabase;
- 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;
- }
- }
- <?php
- namespace AdminModelUser;
- use EngineModel;
- 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();
- }
- }
- <?php
- namespace AdminController;
- class DashboardController extends AdminController
- {
- public function index(){
- $userModel = $this->load->model('User');
- $userModel->repository->test();
- print_r($userModel->repository->getUsers());
- $this->view->render('dashboard');
- }
- }
- $this->sql['set'] .= "SET ";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement