浏览代码

First commit

Artur 7 年之前
父节点
当前提交
c3ccb9520d
共有 5 个文件被更改,包括 456 次插入0 次删除
  1. 17 0
      composer.json
  2. 36 0
      src/BBOrm/Connections.class.php
  3. 28 0
      src/BBOrm/DBInstance.php
  4. 288 0
      src/BBOrm/Entity.class.php
  5. 87 0
      src/index.php

+ 17 - 0
composer.json

@@ -0,0 +1,17 @@
+{
+    "name" : "ahwelp/bborm",
+    "description" : "Diferent ORM",
+    "type" : "library",
+    "authors" : [
+        {
+            "name" : "ahwelp",
+            "email" : "ahwelp@ahwelp.com"
+        }
+    ],
+    "autoload" : {
+        "psr-4" : {
+            "BBRouter\\" : "src/BBOrm/"
+        }
+    },
+    "require" : { }
+}

+ 36 - 0
src/BBOrm/Connections.class.php

@@ -0,0 +1,36 @@
+<?php
+
+namespace BBOrm;
+
+class Connections {
+
+    private static $_connections;
+    private $connections = Array();
+
+    private function __construct() {
+        
+    }
+
+    private static function newObj() {
+        if (!isset(self::$_connections)) {
+            self::$_connections = new Connections();
+        }
+        return self::$_connections;
+    }
+
+    public static function getInstance() {
+        if (!isset(self::$_connections)) {
+            return self::newObj();
+        }
+        return self::$_connections;
+    }
+
+    public static function add_connection($pdo, $name = 'default') {
+        self::getInstance()->connections[$name] = $pdo;
+    }
+
+    public static function get_connection($name = 'default') {
+        return self::getInstance()->connections[$name];
+    }
+
+}

+ 28 - 0
src/BBOrm/DBInstance.php

@@ -0,0 +1,28 @@
+<?php
+
+namespace BBOrm;
+
+class DBInstance {
+
+    public static function execute($sql, $intance = 'default') {
+        $con = Connections::get_connection('default');
+        $con->exec($sql);
+    }
+
+    public static function last_insert($intance = 'default') {
+        $con = Connections::get_connection($intance);
+        return  $con->lastInsertId();
+    }
+
+    public static function query($sql, $instance = 'default') {
+        $con = Connections::get_connection('default');
+        return $con->query($sql, \PDO::FETCH_OBJ);
+    }
+
+    public static function query_prepare($sql, $arguments, $instance = 'default'){
+        $con = Connections::get_connection('default');
+        $statement = $con->prepare($sql);
+        $statement->execute($arguments);
+        return $statement->fetchAll(\PDO::FETCH_OBJ);
+    }
+}

+ 288 - 0
src/BBOrm/Entity.class.php

@@ -0,0 +1,288 @@
+<?php
+
+namespace BBOrm;
+
+abstract class Entity {
+
+    protected $table_name;
+    protected $connection_name = 'default';
+    protected $softdelete = false;
+    private $_ignore = Array(
+        '_ignore',
+        'connection_name',
+        'timestamps',
+        'softdelete',
+        'table_name'
+    );
+
+    function __construct() {
+        if (isset($this->timestamps) && $this->timestamps) {
+            $this->created_at = date('Y-m-d h:m:s');
+            $this->updated_at = date('Y-m-d h:m:s');
+        }
+    }
+
+    public function __set($property, $value) {
+        if (in_array($property, $this->_ignore)) {
+            return;
+        }
+        $this->$property = $value;
+    }
+
+    public function save() {
+        if (isset($this->id)) {
+            $this->update();
+        } else {
+            $this->create();
+        }
+    }
+
+    private function update() {
+        $updatekeys = $this->_ignore;
+        $updatekeys[] = 'id';
+        $columns = array_diff(array_keys(get_object_vars($this)), $updatekeys);
+        $obj = (array) $this;
+
+        $update_string = '';
+
+        foreach ($columns as $column) {
+            $update_string .= $column . ' = ';
+
+            if (is_string($obj[$column])) {
+                $update_string .= "'" . addslashes($obj[$column]) . "', ";
+            } else {
+                $update_string .= $obj[$column] . ", ";
+            }
+        }
+        $update_string = rtrim($update_string, ', ');
+        $sql = "UPDATE $this->table_name SET $update_string WHERE id = $this->id";
+        DBInstance::execute($sql, $this->connection_name);
+    }
+
+    private function create() {
+        $columns = array_diff(array_keys(get_object_vars($this)), $this->_ignore);
+        $obj = (array) $this;
+
+        $first_argument = '';
+        $second_argument = '';
+
+        foreach ($columns as $column) {
+            $first_argument .= $column . ', ';
+
+            if (is_string($obj[$column])) {
+                $second_argument .= "'" . addslashes($obj[$column]) . "', ";
+            } else {
+                $second_argument .= $obj[$column] . ", ";
+            }
+        }
+
+        $first_argument = rtrim($first_argument, ', ');
+        $second_argument = rtrim($second_argument, ', ');
+
+        $sql = "INSERT INTO $this->table_name ($first_argument) VALUES ($second_argument)";
+        DBInstance::execute($sql, $this->connection_name);
+        $this->id = DBInstance::last_insert();
+    }
+
+    public function delete() {
+        if (isset($this->softdelete) && $this->softdelete) {
+            $this->deleted_at = date('Y-m-d h:m:s');
+            $this->update();
+            return;
+        }
+        $this->purge();
+    }
+
+    public function load($id = false) {
+        if (!$id) {
+            return;
+        }
+
+        $sql = "SELECT * FROM $this->table_name WHERE id = ?";
+
+        $this->fill(DBInstance::query_prepare($sql, Array($id), $this->connection_name)[0]);
+    }
+
+    public function purge() {
+        $sql = "DELETE FROM $this->table_name WHERE id = $this->id";
+        DBInstance::execute($sql, $this->connection_name);
+    }
+
+    public function fill($data) {
+        $data = (array) $data;
+
+        foreach ($data as $key => $value) {
+            $this->$key = $value;
+        }
+    }
+
+    public function find_all($select = Array('*'), $limits = Array(), $trashed = false) {
+        $criteria = '';
+        $limits_sql = '';
+
+        foreach ($select as $value) {
+            $criteria .= $value . ', ';
+        }
+
+        $criteria = rtrim($criteria, ', ');
+
+        foreach ($limits as $key => $value) {
+            $limits_sql .= "$key $value ";
+        }
+
+        if ($this->softdelete && !$trashed) {
+            $sql = "SELECT $criteria FROM $this->table_name WHERE deleted_at is null $limits_sql";
+        } else {
+            $sql = "SELECT $criteria FROM $this->table_name $limits_sql";
+        }
+
+        $results = DBInstance::query($sql, $this->connection_name);
+        $objects = Array();
+
+        foreach ($results as $value) {
+            $object = new $this->classname;
+            $object->fill($value);
+            $objects[] = $object;
+        }
+        return $objects;
+    }
+
+    public function find_one($criterias = Array(), $select = Array('*'), $trashed = false) {
+        $select_sql = "";
+        $criteria_sql = "";
+        $limits_sql = "";
+
+        $limits = Array("LIMIT" => 1);
+
+        foreach ($criterias as $key => $criteria) {
+            if ($criteria_sql != "") {
+                $criteria_sql .= " AND ";
+            }
+            if (is_string($criteria[1])) {
+                $criteria_sql .= "$key $criteria[0] '$criteria[1]'";
+            } else {
+                $criteria_sql .= "$key $criteria[0] $criteria[1]";
+            }
+        }
+
+        foreach ($select as $value) {
+            $select_sql .= $value . ', ';
+        }
+        $select_sql = rtrim($select_sql, ', ');
+
+        foreach ($limits as $key => $value) {
+            $limits_sql .= "$key $value ";
+        }
+
+        if ($this->softdelete && !$trashed) {
+            $sql = "SELECT $select_sql FROM $this->table_name WHERE $criteria_sql AND deleted_at is null $limits_sql";
+        } else {
+            $sql = "SELECT $select_sql FROM $this->table_name WHERE $criteria_sql $limits_sql";
+        }
+        $results = DBInstance::query($sql, $this->connection_name);
+        $objects = Array();
+
+        foreach ($results as $value) {
+            $object = new $this->classname;
+            $object->fill($value);
+            $objects[] = $object;
+        }
+        if (empty($objects)) {
+            return new $this->classname;
+        }
+        return $objects;
+    }
+
+    public function find_many($criterias = Array(), $select = Array('*'), $limits = Array(), $trashed = false) {
+        $select_sql = "";
+        $criteria_sql = "";
+        $limits_sql = "";
+
+        foreach ($criterias as $key => $criteria) {
+            if ($criteria_sql != "") {
+                $criteria_sql .= " AND ";
+            }
+            if (is_string($criteria[1])) {
+                $criteria_sql .= "$key $criteria[0] '$criteria[1]'";
+            } else if (is_array($criteria[1])) {
+                $criteria_sql .= "$key $criteria[0] (" . implode(', ', $criteria[1]) . ")";
+            } else {
+                $criteria_sql .= "$key $criteria[0] $criteria[1]";
+            }
+        }
+
+        foreach ($select as $value) {
+            $select_sql .= $value . ', ';
+        }
+        $select_sql = rtrim($select_sql, ', ');
+
+        if ($this->softdelete && !$trashed) {
+            $sql = "SELECT $select_sql FROM $this->table_name WHERE $criteria_sql AND deleted_at is null $limits_sql";
+        } else {
+            $sql = "SELECT $select_sql FROM $this->table_name WHERE $criteria_sql $limits_sql";
+        }
+
+        $results = DBInstance::query($sql, $this->connection_name);
+        $objects = Array();
+
+        foreach ($results as $value) {
+            $object = new $this->classname;
+            $object->fill($value);
+            $objects[] = $object;
+        }
+        if(empty($objects)){
+            return Array();
+        }
+        return $objects;
+    }
+
+    protected function has_one_local($foreign_object, $field_in_local) {
+        $obj = new $foreign_object;
+        return $obj->find_one(Array($field_in_local => Array('=', $this->id)));
+    }
+
+    protected function has_many($foreign_object, $field_in_foreign) {
+        $obj = new $foreign_object;
+        return $obj->find_many(Array($field_in_foreign => Array('=', $this->id)));
+    }
+
+    protected function belongs_to($foreign_object, $local_field) {
+        $obj = new $foreign_object;
+        return $obj->find_one(Array('id' => Array('=', $this->$local_field)));
+    }
+
+    protected function belongs_to_many($foreign_object, $pivot_table, $local_in_pivot, $remote_in_pivot) {
+        $obj = new $foreign_object;
+        $sql = "SELECT $remote_in_pivot FROM $pivot_table WHERE $local_in_pivot = $this->id";
+        
+        $relations = DBInstance::query($sql, $this->connection_name);
+        $ids = Array();
+        foreach ($relations as $relation) {
+            $ids[] = $relation->$remote_in_pivot;
+        }
+        if (empty($ids)) {
+            return Array();
+        }
+        return $obj->find_many(Array('id' => Array('IN', $ids)));
+    }
+
+    protected function belongs_to_many_extended($foreign_object, $pivot_table, $local_in_pivot, $remote_in_pivot) {
+        $obj = new $foreign_object;
+        $sql = "SELECT * FROM $pivot_table WHERE $local_in_pivot = $this->id";
+        $relations = DBInstance::query($sql, $this->connection_name);
+        $objects = Array();
+        
+        if(empty($relations)){ 
+            return Array();
+        }        
+        foreach ($relations as $relation) {
+            $relation->child_element = $obj->find_one(Array("id" => Array("=", $relation->$remote_in_pivot)));
+            $objects[] = $relation;
+        }
+        if (empty($objects)) {
+            return Array();
+        }
+        return $objects;
+    }
+
+}

+ 87 - 0
src/index.php

@@ -0,0 +1,87 @@
+<?php
+
+include_once './Connections.class.php';
+include_once './DBInstance.php';
+include_once './Entity.class.php';
+
+class Estado extends Entity {
+
+    protected $table_name = 'basico_geografico_estados';
+    protected $classname = 'Estado';
+
+    function municipios() {
+        return $this->has_many(Municipio::class, 'estado_id');
+    }
+
+}
+
+class Municipio extends Entity {
+
+    protected $table_name = 'basico_geografico_municipios';
+    protected $classname = 'Municipio';
+
+    function estado() {
+        return $this->belongs_to(Estado::class, 'estado_id');
+    }
+
+}
+
+class User extends Entity {
+
+    protected $timestamps = true;
+    protected $softdelete = true;
+    protected $table_name = 'basico_auth_users';
+    protected $classname = 'User';
+
+    public function existencia() {
+        $this->has_one(Existencia::class, 'user_id');
+    }
+
+    public function contatos() {
+        $this->has_many('contatos', 'user_id');
+    }
+
+    public function permissions() {
+        return $this->belongs_to_many_extended(Permission::class, 'basico_auth_permission_user', 'user_id', 'permission_id');
+    }
+
+    public function roles() {
+        return $this->belongs_to_many( Role::class, 'basico_auth_role_user', 'user_id', 'role_id');
+    }
+
+}
+
+class Role extends Entity{
+    
+    protected $table_name = "basico_auth_roles";
+    protected $classname = "Role";
+    
+    function permissions(){
+        return $this->belongs_to_many(Permission::class, 'basico_auth_role_permission', 'role_id',  'permission_id');
+    }
+}
+
+class Permission extends Entity{
+    
+    protected $table_name = "basico_auth_permissions";
+    protected $classname = "Permission";
+    
+}
+
+Connections::add_connection(new PDO("pgsql:dbname=urfat; user=urfat; password=urfat;host=127.0.0.1;port=5432"));
+
+$a = new User();
+$a->load(1);
+
+var_dump( $a->permissions() );
+
+//$a = new Estado();
+//$a->load(23);
+
+//foreach ($a->municipios() as $municipio) {
+//    echo "$municipio->nome \n";
+//}
+
+//$a = new Municipio();
+//$a->load(1);
+//$a->estado();