Now search methods accept static calls
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace BBOrm;
|
//namespace BBOrm;
|
||||||
|
|
||||||
class Connections {
|
class Connections {
|
||||||
|
|
||||||
|
|||||||
+27
-14
@@ -1,33 +1,46 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace BBOrm;
|
//namespace BBOrm;
|
||||||
|
|
||||||
class DBInstance {
|
class DBInstance {
|
||||||
|
|
||||||
public static function execute($sql, $intance = 'default') {
|
public static function execute($sql, $intance = 'default', $data = null) {
|
||||||
$con = Connections::get_connection('default');
|
if ($data) {
|
||||||
$con->exec($sql);
|
self::query_prepare($sql, $data, $instance);
|
||||||
|
} else {
|
||||||
|
$con = Connections::get_connection('default');
|
||||||
|
$con->exec($sql);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function last_insert($intance = 'default') {
|
public static function last_insert($intance = 'default') {
|
||||||
$con = Connections::get_connection($intance);
|
$con = Connections::get_connection($intance);
|
||||||
return $con->lastInsertId();
|
return $con->lastInsertId();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function query($sql, $instance = 'default') {
|
public static function query($sql, $instance = 'default', $data = null) {
|
||||||
$con = Connections::get_connection('default');
|
if ($data) {
|
||||||
return $con->query($sql, \PDO::FETCH_OBJ);
|
return self::query_prepare($sql, $data, $instance);
|
||||||
}
|
} else {
|
||||||
|
$con = Connections::get_connection('default');
|
||||||
public static function query_one($sql, $instance = 'default') {
|
return $con->query($sql, \PDO::FETCH_OBJ);
|
||||||
$con = Connections::get_connection('default');
|
}
|
||||||
return $con->query($sql, \PDO::FETCH_OBJ)->fetch();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function query_prepare($sql, $arguments, $instance = 'default'){
|
public static function query_one($sql, $instance = 'default', $data = null) {
|
||||||
|
if ($data) {
|
||||||
|
return self::query_prepare($sql, $data, $instance);
|
||||||
|
} else {
|
||||||
|
$con = Connections::get_connection('default');
|
||||||
|
return $con->query($sql, \PDO::FETCH_OBJ)->fetch();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function query_prepare($sql, $arguments, $instance = 'default') {
|
||||||
$con = Connections::get_connection('default');
|
$con = Connections::get_connection('default');
|
||||||
$statement = $con->prepare($sql);
|
$statement = $con->prepare($sql);
|
||||||
$statement->execute($arguments);
|
$statement->execute($arguments);
|
||||||
return $statement->fetchAll(\PDO::FETCH_OBJ);
|
return $statement->fetchAll(\PDO::FETCH_OBJ);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+234
-86
@@ -1,13 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace BBOrm;
|
//namespace BBOrm;
|
||||||
|
|
||||||
abstract class Entity {
|
abstract class Entity {
|
||||||
|
|
||||||
protected $table_name;
|
const table_name = '';
|
||||||
protected $connection_name = 'default';
|
const connection_name = 'default';
|
||||||
protected $softdelete = false;
|
const softdelete = false;
|
||||||
private $_ignore = Array(
|
const _ignore = Array(
|
||||||
'classname',
|
'classname',
|
||||||
'_properties',
|
'_properties',
|
||||||
'_ignore',
|
'_ignore',
|
||||||
@@ -16,19 +16,23 @@ abstract class Entity {
|
|||||||
'softdelete',
|
'softdelete',
|
||||||
'table_name'
|
'table_name'
|
||||||
);
|
);
|
||||||
|
|
||||||
protected $_properties = Array();
|
protected $_properties = Array();
|
||||||
|
|
||||||
public function get_properties(){
|
public function teste() {
|
||||||
|
return static::class;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get_properties() {
|
||||||
$properties = Array();
|
$properties = Array();
|
||||||
foreach ($this->_properties as $propertie){
|
foreach ($this->_properties as $propertie) {
|
||||||
$obj = new \stdClass();
|
$obj = new \stdClass();
|
||||||
$obj->data = $propertie;
|
$obj->data = $propertie;
|
||||||
$properties[] = $obj;
|
$properties[] = $obj;
|
||||||
}
|
}
|
||||||
return json_encode($properties);
|
return json_encode($properties);
|
||||||
}
|
}
|
||||||
|
|
||||||
function __construct() {
|
function __construct() {
|
||||||
if (isset($this->timestamps) && $this->timestamps) {
|
if (isset($this->timestamps) && $this->timestamps) {
|
||||||
$this->created_at = date('Y-m-d h:m:s');
|
$this->created_at = date('Y-m-d h:m:s');
|
||||||
@@ -37,7 +41,7 @@ abstract class Entity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function __set($property, $value) {
|
public function __set($property, $value) {
|
||||||
if (in_array($property, $this->_ignore)) {
|
if (in_array($property, static::_ignore)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$this->$property = $value;
|
$this->$property = $value;
|
||||||
@@ -52,49 +56,46 @@ abstract class Entity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function update() {
|
private function update() {
|
||||||
$updatekeys = $this->_ignore;
|
$updatekeys = static::_ignore;
|
||||||
$updatekeys[] = 'id';
|
$updatekeys[] = 'id';
|
||||||
$columns = array_diff(array_keys(get_object_vars($this)), $updatekeys);
|
$columns = array_diff(array_keys(get_object_vars($this)), $updatekeys);
|
||||||
$obj = (array) $this;
|
$obj = (array) $this;
|
||||||
|
|
||||||
$update_string = '';
|
$update_string = '';
|
||||||
|
$update_data = Array();
|
||||||
|
$update_data['_update_id'] = $this->id;
|
||||||
|
|
||||||
foreach ($columns as $column) {
|
foreach ($columns as $column) {
|
||||||
$update_string .= $column . ' = ';
|
$update_string .= $column . ":$column, ";
|
||||||
|
$update_data[$column] = $obj[$column];
|
||||||
if (is_string($obj[$column])) {
|
|
||||||
$update_string .= "'" . addslashes($obj[$column]) . "', ";
|
|
||||||
} else {
|
|
||||||
$update_string .= $obj[$column] . ", ";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$update_string = rtrim($update_string, ', ');
|
$update_string = rtrim($update_string, ', ');
|
||||||
$sql = "UPDATE $this->table_name SET $update_string WHERE id = $this->id";
|
|
||||||
DBInstance::execute($sql, $this->connection_name);
|
$sql = "UPDATE " . static::table_name . " SET $update_string WHERE id = :_update_id";
|
||||||
|
DBInstance::execute($sql, $this->connection_name, $update_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function create() {
|
private function create() {
|
||||||
$columns = array_diff(array_keys(get_object_vars($this)), $this->_ignore);
|
$columns = array_diff(array_keys(get_object_vars($this)), static::_ignore);
|
||||||
$obj = (array) $this;
|
$obj = (array) $this;
|
||||||
|
|
||||||
$first_argument = '';
|
$first_argument = '';
|
||||||
$second_argument = '';
|
$second_argument = '';
|
||||||
|
$insert_data = Array();
|
||||||
|
|
||||||
foreach ($columns as $column) {
|
foreach ($columns as $column) {
|
||||||
$first_argument .= $column . ', ';
|
$first_argument .= $column . ', ';
|
||||||
|
|
||||||
if (is_string($obj[$column])) {
|
$insert_data[$column] = $obj[$column];
|
||||||
$second_argument .= "'" . addslashes($obj[$column]) . "', ";
|
$second_argument .= ":$column, ";
|
||||||
} else {
|
|
||||||
$second_argument .= $obj[$column] . ", ";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$first_argument = rtrim($first_argument, ', ');
|
$first_argument = rtrim($first_argument, ', ');
|
||||||
$second_argument = rtrim($second_argument, ', ');
|
$second_argument = rtrim($second_argument, ', ');
|
||||||
|
|
||||||
$sql = "INSERT INTO $this->table_name ($first_argument) VALUES ($second_argument)";
|
$sql = "INSERT INTO " . static::table_name . " ($first_argument) VALUES ($second_argument)";
|
||||||
DBInstance::execute($sql, $this->connection_name);
|
DBInstance::execute($sql, static::connection_name, $insert_data);
|
||||||
$this->id = DBInstance::last_insert();
|
$this->id = DBInstance::last_insert();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,17 +113,17 @@ abstract class Entity {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$sql = "SELECT * FROM $this->table_name WHERE id = ?";
|
$sql = "SELECT * FROM " . static::table_name . " WHERE id = ?";
|
||||||
|
|
||||||
$this->fill(DBInstance::query_prepare($sql, Array($id), $this->connection_name)[0]);
|
$this->fill(DBInstance::query_prepare($sql, Array($id), static::connection_name)[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function purge() {
|
public function purge() {
|
||||||
$sql = "DELETE FROM $this->table_name WHERE id = $this->id";
|
$sql = "DELETE FROM " . static::table_name . " WHERE id = :id";
|
||||||
DBInstance::execute($sql, $this->connection_name);
|
DBInstance::execute($sql, static::connection_name, Array('id' => $this->id));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function fill($data) {
|
private function fill($data) {
|
||||||
$data = (array) $data;
|
$data = (array) $data;
|
||||||
|
|
||||||
foreach ($data as $key => $value) {
|
foreach ($data as $key => $value) {
|
||||||
@@ -130,7 +131,17 @@ abstract class Entity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function find_all($select = Array('*'), $limits = Array(), $trashed = false) {
|
/**
|
||||||
|
* Find All
|
||||||
|
*
|
||||||
|
* This method will load all instances from the class.
|
||||||
|
* Limits are avaliable for paginations.
|
||||||
|
*
|
||||||
|
* @param Array $select Columns to select. Array('id', 'name')
|
||||||
|
* @param Array $limits Array( 'offset'=> 10, 'limit' => 10 )
|
||||||
|
* @param boolean $trashed Bring trashed elements?
|
||||||
|
*/
|
||||||
|
public static function find_all($select = Array('*'), $limits = Array(), $trashed = false) {
|
||||||
$criteria = '';
|
$criteria = '';
|
||||||
$limits_sql = '';
|
$limits_sql = '';
|
||||||
|
|
||||||
@@ -144,71 +155,108 @@ abstract class Entity {
|
|||||||
$limits_sql .= "$key $value ";
|
$limits_sql .= "$key $value ";
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->softdelete && !$trashed) {
|
if (static::softdelete && !$trashed) {
|
||||||
$sql = "SELECT $criteria FROM $this->table_name WHERE deleted_at is null $limits_sql";
|
$sql = "SELECT $criteria FROM " . static::table_name . " WHERE deleted_at is null $limits_sql";
|
||||||
} else {
|
} else {
|
||||||
$sql = "SELECT $criteria FROM $this->table_name $limits_sql";
|
$sql = "SELECT $criteria FROM " . static::table_name . " $limits_sql";
|
||||||
}
|
}
|
||||||
|
|
||||||
$results = DBInstance::query($sql, $this->connection_name);
|
$results = DBInstance::query($sql, static::connection_name);
|
||||||
$objects = Array();
|
$objects = Array();
|
||||||
|
|
||||||
foreach ($results as $value) {
|
foreach ($results as $value) {
|
||||||
$object = new $this->classname;
|
$static = static::class;
|
||||||
|
$object = new $static;
|
||||||
$object->fill($value);
|
$object->fill($value);
|
||||||
$objects[] = $object;
|
$objects[] = $object;
|
||||||
}
|
}
|
||||||
return $objects;
|
return $objects;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function count($criterias = Array(), $trashed = false) {
|
/**
|
||||||
|
* Count
|
||||||
|
*
|
||||||
|
* Count how many records there is on the database
|
||||||
|
* in relation with this class with the parameters filter
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param Array $criterias Criteras for WHERE Array('id' => Array('=', 10) )
|
||||||
|
* @param boolean $trashed Bring trashed registers?
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static function count($criterias = Array(), $trashed = false) {
|
||||||
$criteria_sql = "";
|
$criteria_sql = "";
|
||||||
|
$criteria_data = Array();
|
||||||
foreach ($criterias as $key => $criteria) {
|
foreach ($criterias as $key => $criteria) {
|
||||||
if ($criteria_sql != "") {
|
if ($criteria_sql != "") {
|
||||||
$criteria_sql .= " AND ";
|
$criteria_sql .= " AND ";
|
||||||
} else {
|
} else {
|
||||||
$criteria_sql .= " WHERE ";
|
$criteria_sql .= " WHERE ";
|
||||||
}
|
}
|
||||||
|
if (is_array($criteria[1])) {
|
||||||
if (is_string($criteria[1])) {
|
$crit_temp = '';
|
||||||
$criteria_sql .= "$key $criteria[0] '$criteria[1]'";
|
foreach ($criteria[1] as $crit) {
|
||||||
|
$crit_temp .= '?, ';
|
||||||
|
$criteria_data[] = $crit;
|
||||||
|
}
|
||||||
|
$crit_temp = rtrim($crit_temp, ', ');
|
||||||
|
$criteria_sql .= "$key $criteria[0] ($crit_temp)";
|
||||||
} else {
|
} else {
|
||||||
$criteria_sql .= "$key $criteria[0] $criteria[1]";
|
$criteria_sql .= "$key $criteria[0] (?) ";
|
||||||
|
$criteria_data[] = $criteria[1];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (static::softdelete && !$trashed) {
|
||||||
if ($this->softdelete && !$trashed) {
|
$sql = "SELECT COUNT(*) FROM " . static::table_name . " $criteria_sql AND deleted_at is null";
|
||||||
$sql = "SELECT COUNT(*) FROM $this->table_name $criteria_sql AND deleted_at is null";
|
|
||||||
} else {
|
} else {
|
||||||
$sql = "SELECT COUNT(*) FROM $this->table_name $criteria_sql";
|
$sql = "SELECT COUNT(*) FROM " . static::table_name . " $criteria_sql";
|
||||||
}
|
}
|
||||||
|
|
||||||
$results = DBInstance::query_one($sql, $this->connection_name);
|
$results = DBInstance::query_one($sql, static::connection_name, $criteria_data);
|
||||||
|
return $results[0]->count;
|
||||||
return $results->count;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function find_one($criterias = Array(), $select = Array('*'), $trashed = false) {
|
/**
|
||||||
|
* Find One
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param Array/Int $criterias Array('id' => Array('in', Array(10, 20, 30)))
|
||||||
|
* @param Array $select Array(id, fullname)
|
||||||
|
* @param Boolan $trashed true, false
|
||||||
|
*/
|
||||||
|
public static function find_one($criterias = Array(), $select = Array('*'), $trashed = false) {
|
||||||
$select_sql = "";
|
$select_sql = "";
|
||||||
$criteria_sql = "";
|
$criteria_sql = "";
|
||||||
$limits_sql = "";
|
$limits_sql = "";
|
||||||
|
$criteria_data = Array();
|
||||||
$limits = Array("LIMIT" => 1);
|
|
||||||
|
|
||||||
foreach ($criterias as $key => $criteria) {
|
$limits = Array("LIMIT" => 1);
|
||||||
if ($criteria_sql != "") {
|
|
||||||
$criteria_sql .= " AND ";
|
if (is_numeric($criterias)) {
|
||||||
} else {
|
$criteria_sql = "WHERE id = ? ";
|
||||||
$criteria_sql .= " WHERE ";
|
$criteria_data[] = $criterias;
|
||||||
}
|
} else {
|
||||||
if (is_string($criteria[1])) {
|
foreach ($criterias as $key => $criteria) {
|
||||||
$criteria_sql .= "$key $criteria[0] '$criteria[1]'";
|
if ($criteria_sql != "") {
|
||||||
} else {
|
$criteria_sql .= " AND ";
|
||||||
$criteria_sql .= "$key $criteria[0] $criteria[1]";
|
} else {
|
||||||
|
$criteria_sql .= " WHERE ";
|
||||||
|
}
|
||||||
|
if (is_array($criteria[1])) {
|
||||||
|
$crit_temp = '';
|
||||||
|
foreach ($criteria[1] as $crit) {
|
||||||
|
$crit_temp .= '?, ';
|
||||||
|
$criteria_data[] = $crit;
|
||||||
|
}
|
||||||
|
$crit_temp = rtrim($crit_temp, ', ');
|
||||||
|
$criteria_sql .= "$key $criteria[0] ($crit_temp)";
|
||||||
|
} else {
|
||||||
|
$criteria_sql .= "$key $criteria[0] (?) ";
|
||||||
|
$criteria_data[] = $criteria[1];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($select as $value) {
|
foreach ($select as $value) {
|
||||||
$select_sql .= $value . ', ';
|
$select_sql .= $value . ', ';
|
||||||
}
|
}
|
||||||
@@ -218,16 +266,18 @@ abstract class Entity {
|
|||||||
$limits_sql .= "$key $value ";
|
$limits_sql .= "$key $value ";
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->softdelete && !$trashed) {
|
if (static::softdelete && !$trashed) {
|
||||||
$sql = "SELECT $select_sql FROM $this->table_name $criteria_sql AND deleted_at is null $limits_sql";
|
$sql = "SELECT $select_sql FROM " . static::table_name . " $criteria_sql AND deleted_at is null $limits_sql";
|
||||||
} else {
|
} else {
|
||||||
$sql = "SELECT $select_sql FROM $this->table_name $criteria_sql $limits_sql";
|
$sql = "SELECT $select_sql FROM " . static::table_name . " $criteria_sql $limits_sql";
|
||||||
}
|
}
|
||||||
$results = DBInstance::query($sql, $this->connection_name);
|
|
||||||
|
$results = DBInstance::query($sql, static::connection_name, $criteria_data);
|
||||||
$objects = Array();
|
$objects = Array();
|
||||||
|
|
||||||
foreach ($results as $value) {
|
foreach ($results as $value) {
|
||||||
$object = new $this->classname;
|
$class = static::class;
|
||||||
|
$object = new $class;
|
||||||
$object->fill($value);
|
$object->fill($value);
|
||||||
$objects[] = $object;
|
$objects[] = $object;
|
||||||
}
|
}
|
||||||
@@ -237,7 +287,16 @@ abstract class Entity {
|
|||||||
return $objects[0];
|
return $objects[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function find_many($criterias = Array(), $select = Array('*'), $limits = Array(), $trashed = false) {
|
/**
|
||||||
|
* Find Many
|
||||||
|
*
|
||||||
|
* @param Array $criterias Description
|
||||||
|
* @param Array $select Description
|
||||||
|
* @param Array $limits Description
|
||||||
|
* @param boolean $trashed Description
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static function find_many($criterias = Array(), $select = Array('*'), $limits = Array(), $trashed = false) {
|
||||||
$select_sql = "";
|
$select_sql = "";
|
||||||
$criteria_sql = "";
|
$criteria_sql = "";
|
||||||
$limits_sql = "";
|
$limits_sql = "";
|
||||||
@@ -266,17 +325,18 @@ abstract class Entity {
|
|||||||
}
|
}
|
||||||
$select_sql = rtrim($select_sql, ', ');
|
$select_sql = rtrim($select_sql, ', ');
|
||||||
|
|
||||||
if ($this->softdelete && !$trashed) {
|
if (static::softdelete && !$trashed) {
|
||||||
$sql = "SELECT $select_sql FROM $this->table_name $criteria_sql AND deleted_at is null $limits_sql";
|
$sql = "SELECT $select_sql FROM " . static::table_name . " $criteria_sql AND deleted_at is null $limits_sql";
|
||||||
} else {
|
} else {
|
||||||
$sql = "SELECT $select_sql FROM $this->table_name $criteria_sql $limits_sql";
|
$sql = "SELECT $select_sql FROM " . static::table_name . " $criteria_sql $limits_sql";
|
||||||
}
|
}
|
||||||
|
|
||||||
$results = DBInstance::query($sql, $this->connection_name);
|
$results = DBInstance::query($sql, static::connection_name);
|
||||||
$objects = Array();
|
$objects = Array();
|
||||||
|
|
||||||
foreach ($results as $value) {
|
foreach ($results as $value) {
|
||||||
$object = new $this->classname;
|
$class = static::class;
|
||||||
|
$object = new $class;
|
||||||
$object->fill($value);
|
$object->fill($value);
|
||||||
$objects[] = $object;
|
$objects[] = $object;
|
||||||
}
|
}
|
||||||
@@ -286,31 +346,99 @@ abstract class Entity {
|
|||||||
return $objects;
|
return $objects;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function has_one_local($foreign_object, $field_in_local) {
|
/**
|
||||||
|
* Has One Local
|
||||||
|
*
|
||||||
|
* Defines that, this object has a child an only one,
|
||||||
|
* object in other class.
|
||||||
|
*
|
||||||
|
* Table user = (id, name)
|
||||||
|
* Table phone = (id, number, userid)
|
||||||
|
*
|
||||||
|
* This relation will be created on the User class
|
||||||
|
* making reference to the Phone class
|
||||||
|
*
|
||||||
|
* @param Object $foreign_object Class instance from the remote object
|
||||||
|
* @param (int, string) $field_in_remote Field in local object matchin the remote id
|
||||||
|
*
|
||||||
|
* @return Object Instance of the remote class
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
protected function has_one($foreign_object, $field_in_remote) {
|
||||||
$obj = new $foreign_object;
|
$obj = new $foreign_object;
|
||||||
return $obj->find_one(Array($field_in_local => Array('=', $this->id)));
|
return $obj->find_one(Array($field_in_remote => Array('=', $this->id)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Has Many
|
||||||
|
*
|
||||||
|
* Defines that, this object has many instances of other Object
|
||||||
|
*
|
||||||
|
* Table Post = (id, name, content)
|
||||||
|
* Table Comment = (id, name, content, postid)
|
||||||
|
*
|
||||||
|
* This relation will be created on the Post class
|
||||||
|
* Making reference to the Comment class
|
||||||
|
*
|
||||||
|
* @param type $foreign_object Instance of a remote class
|
||||||
|
* @param (int, string) $field_in_foreign The field to match the local id
|
||||||
|
*
|
||||||
|
*/
|
||||||
protected function has_many($foreign_object, $field_in_foreign) {
|
protected function has_many($foreign_object, $field_in_foreign) {
|
||||||
$obj = new $foreign_object;
|
$obj = new $foreign_object;
|
||||||
return $obj->find_many(Array($field_in_foreign => Array('=', $this->id)));
|
return $obj->find_many(Array($field_in_foreign => Array('=', $this->id)));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function belongs_to($foreign_object, $local_field) {
|
/**
|
||||||
|
* Belongs To
|
||||||
|
*
|
||||||
|
* Defines that, this object is part of an other class.
|
||||||
|
* The local field must match the id of an other class.
|
||||||
|
*
|
||||||
|
* Table Post = (id, name, content)
|
||||||
|
* Table Comment = (id, name, content, postid)
|
||||||
|
*
|
||||||
|
* This relation will be created on the comment class
|
||||||
|
* Making reference to the post class
|
||||||
|
*
|
||||||
|
* @param Object $foreign_object Instance of a remote class
|
||||||
|
* @param (int, String) $local_field Remote field relate to local Object
|
||||||
|
* @param string $remote_field
|
||||||
|
*/
|
||||||
|
protected function belongs_to($foreign_object, $local_field, $remote_field = 'id') {
|
||||||
$obj = new $foreign_object;
|
$obj = new $foreign_object;
|
||||||
return $obj->find_one(Array('id' => Array('=', $this->$local_field)));
|
return $obj->find_one(Array($remote_field => Array('=', $this->$local_field)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Belongs to Many
|
||||||
|
*
|
||||||
|
* Defines that this object is related to many other instances
|
||||||
|
* of other classes throw a pivot table.
|
||||||
|
*
|
||||||
|
* Table Post = (id, name, content)
|
||||||
|
* Table Tag = (id, name)
|
||||||
|
* Table Post_Tag = (id, postid, tagid)
|
||||||
|
*
|
||||||
|
* This relation will be created in both classes
|
||||||
|
*
|
||||||
|
* @param Object $foreign_object Instance of the remote class
|
||||||
|
* @param string $pivot_table Name of the pivot table
|
||||||
|
* @param int $local_in_pivot Name of field on the pivot in relation of the local class
|
||||||
|
* @param int $remote_in_pivot Name of field on the pivot in relation of the remote class
|
||||||
|
* @param Array $remote_filter Filters to the remote Array('id', Array('>', 50) )
|
||||||
|
* @param Array $remote_limit Array( 'offset'=> 10, 'limit' => 10 )
|
||||||
|
*/
|
||||||
protected function belongs_to_many($foreign_object, $pivot_table, $local_in_pivot, $remote_in_pivot, $remote_filter = Array(), $remote_limit = Array()) {
|
protected function belongs_to_many($foreign_object, $pivot_table, $local_in_pivot, $remote_in_pivot, $remote_filter = Array(), $remote_limit = Array()) {
|
||||||
$obj = new $foreign_object;
|
$obj = new $foreign_object;
|
||||||
$limits_sql = '';
|
$limits_sql = '';
|
||||||
|
|
||||||
foreach ($remote_limit as $key => $value) {
|
foreach ($remote_limit as $key => $value) {
|
||||||
$limits_sql .= "$key $value ";
|
$limits_sql .= "$key $value ";
|
||||||
}
|
}
|
||||||
$sql = "SELECT $remote_in_pivot FROM $pivot_table WHERE $local_in_pivot = $this->id $limits_sql";
|
$sql = "SELECT $remote_in_pivot FROM $pivot_table WHERE $local_in_pivot = $this->id $limits_sql";
|
||||||
|
|
||||||
$relations = DBInstance::query($sql, $this->connection_name);
|
$relations = DBInstance::query($sql, static::connection_name);
|
||||||
$ids = Array();
|
$ids = Array();
|
||||||
foreach ($relations as $relation) {
|
foreach ($relations as $relation) {
|
||||||
$ids[] = $relation->$remote_in_pivot;
|
$ids[] = $relation->$remote_in_pivot;
|
||||||
@@ -321,6 +449,26 @@ abstract class Entity {
|
|||||||
return $obj->find_many(array_merge(Array('id' => Array('IN', $ids)), $remote_filter), Array('*'), $remote_limit);
|
return $obj->find_many(array_merge(Array('id' => Array('IN', $ids)), $remote_filter), Array('*'), $remote_limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Belongs to Many Extended
|
||||||
|
*
|
||||||
|
* Defines that this object is related to many other instances
|
||||||
|
* of other classes throw a pivot table.
|
||||||
|
* This relation will bring the pivot table with the elements inside
|
||||||
|
*
|
||||||
|
* Table Post = (id, name, content)
|
||||||
|
* Table Tag = (id, name)
|
||||||
|
* Table Post_Tag = (id, postid, tagid)
|
||||||
|
*
|
||||||
|
* This relation will be created in both classes
|
||||||
|
*
|
||||||
|
* @param Object $foreign_object Instance of the remote class
|
||||||
|
* @param string $pivot_table Name of the pivot table
|
||||||
|
* @param int $local_in_pivot Name of field on the pivot in relation of the local class
|
||||||
|
* @param int $remote_in_pivot Name of field on the pivot in relation of the remote class
|
||||||
|
* @param Array $remote_filter Filters to the remote Array('id', Array('>', 50) )
|
||||||
|
* @param Array $pivot_limits Array( 'offset'=> 10, 'limit' => 10 )
|
||||||
|
*/
|
||||||
protected function belongs_to_many_extended($foreign_object, $pivot_table, $local_in_pivot, $remote_in_pivot, $remote_filter = Array(), $pivot_limits = Array()) {
|
protected function belongs_to_many_extended($foreign_object, $pivot_table, $local_in_pivot, $remote_in_pivot, $remote_filter = Array(), $pivot_limits = Array()) {
|
||||||
$obj = new $foreign_object;
|
$obj = new $foreign_object;
|
||||||
$limits_sql = '';
|
$limits_sql = '';
|
||||||
@@ -329,14 +477,14 @@ abstract class Entity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$sql = "SELECT * FROM $pivot_table WHERE $local_in_pivot = $this->id $limits_sql";
|
$sql = "SELECT * FROM $pivot_table WHERE $local_in_pivot = $this->id $limits_sql";
|
||||||
$relations = DBInstance::query($sql, $this->connection_name);
|
$relations = DBInstance::query($sql, static::connection_name);
|
||||||
$objects = Array();
|
$objects = Array();
|
||||||
|
|
||||||
if (empty($relations)) {
|
if (empty($relations)) {
|
||||||
return Array();
|
return Array();
|
||||||
}
|
}
|
||||||
foreach ($relations as $relation) {
|
foreach ($relations as $relation) {
|
||||||
$relation->child_element = $obj->find_one(array_merge(Array("id" => Array("=", $relation->$remote_in_pivot)), $remote_filter ));
|
$relation->child_element = $obj->find_one(array_merge(Array("id" => Array("=", $relation->$remote_in_pivot)), $remote_filter));
|
||||||
$objects[] = $relation;
|
$objects[] = $relation;
|
||||||
}
|
}
|
||||||
if (empty($objects)) {
|
if (empty($objects)) {
|
||||||
|
|||||||
+32
-21
@@ -1,14 +1,19 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
include_once './Connections.class.php';
|
include_once 'BBOrm/Connections.php';
|
||||||
include_once './DBInstance.php';
|
include_once 'BBOrm/DBInstance.php';
|
||||||
include_once './Entity.class.php';
|
include_once 'BBOrm/Entity.php';
|
||||||
|
|
||||||
|
class Pais extends Entity{
|
||||||
|
|
||||||
|
const table_name = 'basico_geografico_paises';
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
class Estado extends Entity {
|
class Estado extends Entity {
|
||||||
|
|
||||||
protected $table_name = 'basico_geografico_estados';
|
const table_name = 'basico_geografico_estados';
|
||||||
protected $classname = 'Estado';
|
|
||||||
|
|
||||||
function municipios() {
|
function municipios() {
|
||||||
return $this->has_many(Municipio::class, 'estado_id');
|
return $this->has_many(Municipio::class, 'estado_id');
|
||||||
}
|
}
|
||||||
@@ -17,9 +22,8 @@ class Estado extends Entity {
|
|||||||
|
|
||||||
class Municipio extends Entity {
|
class Municipio extends Entity {
|
||||||
|
|
||||||
protected $table_name = 'basico_geografico_municipios';
|
const table_name = 'basico_geografico_municipios';
|
||||||
protected $classname = 'Municipio';
|
|
||||||
|
|
||||||
function estado() {
|
function estado() {
|
||||||
return $this->belongs_to(Estado::class, 'estado_id');
|
return $this->belongs_to(Estado::class, 'estado_id');
|
||||||
}
|
}
|
||||||
@@ -28,11 +32,10 @@ class Municipio extends Entity {
|
|||||||
|
|
||||||
class User extends Entity {
|
class User extends Entity {
|
||||||
|
|
||||||
protected $timestamps = true;
|
const timestamps = true;
|
||||||
protected $softdelete = true;
|
const softdelete = true;
|
||||||
protected $table_name = 'basico_auth_users';
|
const table_name = 'basico_auth_users';
|
||||||
protected $classname = 'User';
|
|
||||||
|
|
||||||
public function existencia() {
|
public function existencia() {
|
||||||
$this->has_one(Existencia::class, 'user_id');
|
$this->has_one(Existencia::class, 'user_id');
|
||||||
}
|
}
|
||||||
@@ -53,8 +56,7 @@ class User extends Entity {
|
|||||||
|
|
||||||
class Role extends Entity{
|
class Role extends Entity{
|
||||||
|
|
||||||
protected $table_name = "basico_auth_roles";
|
const table_name = "basico_auth_roles";
|
||||||
protected $classname = "Role";
|
|
||||||
|
|
||||||
function permissions(){
|
function permissions(){
|
||||||
return $this->belongs_to_many(Permission::class, 'basico_auth_role_permission', 'role_id', 'permission_id');
|
return $this->belongs_to_many(Permission::class, 'basico_auth_role_permission', 'role_id', 'permission_id');
|
||||||
@@ -63,17 +65,26 @@ class Role extends Entity{
|
|||||||
|
|
||||||
class Permission extends Entity{
|
class Permission extends Entity{
|
||||||
|
|
||||||
protected $table_name = "basico_auth_permissions";
|
const 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"));
|
Connections::add_connection(new PDO("pgsql:dbname=urfat; user=urfat; password=urfat;host=127.0.0.1;port=5432"));
|
||||||
|
|
||||||
$a = new User();
|
//$a = new Pais();
|
||||||
$a->load(1);
|
//$b = $a->count( Array('id' => Array('in', Array(10, 20, 30)) ));
|
||||||
|
//$b = $a->find_many( Array('id' => Array('in', Array(10, 20, 30)) ) );
|
||||||
|
//$b = $a->teste();
|
||||||
|
//var_dump( Pais::find_all() );
|
||||||
|
//var_dump( Pais::find_one( Array('id'=> Array('=', 60) ), Array('id') ) );
|
||||||
|
//var_dump( Pais::find_many( Array('id'=> Array('<', 10) ) ) );
|
||||||
|
|
||||||
var_dump( $a->permissions() );
|
//$a = new User();
|
||||||
|
//$a->load(1);
|
||||||
|
//$a = User::find_one(Array('id'=>Array('=', 1) ));
|
||||||
|
$a = User::find_one( 1 );
|
||||||
|
|
||||||
|
var_dump( $a->roles() );
|
||||||
|
|
||||||
//$a = new Estado();
|
//$a = new Estado();
|
||||||
//$a->load(23);
|
//$a->load(23);
|
||||||
|
|||||||
Reference in New Issue
Block a user