Changes
This commit is contained in:
@@ -16,6 +16,16 @@ class BelongsTo {
|
|||||||
$this->localElement = $localElement;
|
$this->localElement = $localElement;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function __call($name, $arguments)
|
||||||
|
{
|
||||||
|
return $this->get()->$name($arguments);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __get($key)
|
||||||
|
{
|
||||||
|
return $this->get()->$key;
|
||||||
|
}
|
||||||
|
|
||||||
function get() {
|
function get() {
|
||||||
return $this->foreignObject::findOne(Array($this->remoteField => Array('=', $this->localElement->{$this->localField})));
|
return $this->foreignObject::findOne(Array($this->remoteField => Array('=', $this->localElement->{$this->localField})));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ class BelongsToMany {
|
|||||||
$this->remoteLimit = $remoteLimit;
|
$this->remoteLimit = $remoteLimit;
|
||||||
}
|
}
|
||||||
|
|
||||||
function get() {
|
function get() : Collection {
|
||||||
|
|
||||||
$limitsSql = '';
|
$limitsSql = '';
|
||||||
foreach ($this->remoteLimit as $key => $value) {
|
foreach ($this->remoteLimit as $key => $value) {
|
||||||
@@ -40,7 +40,7 @@ class BelongsToMany {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (empty($ids)) {
|
if (empty($ids)) {
|
||||||
return Array();
|
return new Collection();
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->foreignObject::findMany(array_merge(Array('id' => Array('IN', $ids)), $this->remoteFilter), Array('*'), $this->remoteLimit);
|
return $this->foreignObject::findMany(array_merge(Array('id' => Array('IN', $ids)), $this->remoteFilter), Array('*'), $this->remoteLimit);
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ class BelongsToManyExtended {
|
|||||||
$this->softDelete = $softDelete;
|
$this->softDelete = $softDelete;
|
||||||
}
|
}
|
||||||
|
|
||||||
function get(){
|
function get() : Collection {
|
||||||
|
|
||||||
$limitsSql = '';
|
$limitsSql = '';
|
||||||
|
|
||||||
@@ -42,10 +42,10 @@ class BelongsToManyExtended {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$relations = DBInstance::query($sql, Array($this->localObject->id), $this->localObject::_connectionName);
|
$relations = DBInstance::query($sql, Array($this->localObject->id), $this->localObject::_connectionName);
|
||||||
$objects = Array();
|
$objects = new Collection();
|
||||||
|
|
||||||
if (empty($relations)) {
|
if (empty($relations)) {
|
||||||
return Array();
|
return new Collection();
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($relations as $relation) {
|
foreach ($relations as $relation) {
|
||||||
@@ -54,7 +54,7 @@ class BelongsToManyExtended {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (empty($objects)) {
|
if (empty($objects)) {
|
||||||
return Array();
|
return new Collection();
|
||||||
}
|
}
|
||||||
|
|
||||||
return $objects;
|
return $objects;
|
||||||
|
|||||||
@@ -26,13 +26,11 @@ class Collection implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
}
|
}
|
||||||
|
|
||||||
// https://www.php.net/manual/en/class.iteratoraggregate.php
|
// https://www.php.net/manual/en/class.iteratoraggregate.php
|
||||||
public function getIterator()
|
public function getIterator(): \Generator
|
||||||
{
|
{
|
||||||
return (function () {
|
foreach ($this->items as $key => $val) {
|
||||||
while (list($key, $val) = each($this->items)) {
|
|
||||||
yield $key => $val;
|
yield $key => $val;
|
||||||
}
|
}
|
||||||
})();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://www.php.net/manual/en/class.arrayaccess.php
|
// https://www.php.net/manual/en/class.arrayaccess.php
|
||||||
@@ -120,7 +118,7 @@ class Collection implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->items;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
function first()
|
function first()
|
||||||
|
|||||||
+74
-45
@@ -2,10 +2,19 @@
|
|||||||
|
|
||||||
namespace ORM;
|
namespace ORM;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Summary of DBInstance
|
||||||
|
*/
|
||||||
class DBInstance
|
class DBInstance
|
||||||
{
|
{
|
||||||
|
|
||||||
private static function addPrefix($sql, $instance = 'default')
|
/**
|
||||||
|
* Summary of addPrefix
|
||||||
|
* @param string $sql
|
||||||
|
* @param string $instance
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private static function addPrefix(string $sql, string $instance = 'default')
|
||||||
{
|
{
|
||||||
$con = Connections::getConnection($instance);
|
$con = Connections::getConnection($instance);
|
||||||
|
|
||||||
@@ -25,14 +34,11 @@ class DBInstance
|
|||||||
*
|
*
|
||||||
* This method will execute a SQL on the database
|
* This method will execute a SQL on the database
|
||||||
*
|
*
|
||||||
* @ctag DBInstance::execute($sql);
|
* @param string $sql The SQL to be executed
|
||||||
* @ctag DBInstance::execute($sql, []);
|
* @param array $data Pass the info to be replaced on the query_prepare
|
||||||
*
|
* @param string $instance Select the connection to execute
|
||||||
* @param String $sql The SQL to be executed
|
|
||||||
* @param Array $data Pass the info to be replaced on the query_prepare
|
|
||||||
* @param String $instance Select the connection to execute
|
|
||||||
*/
|
*/
|
||||||
public static function execute($sql, $data = null, $instance = 'default')
|
public static function execute(string $sql, Array $data = null, string $instance = 'default')
|
||||||
{
|
{
|
||||||
$sql = self::addPrefix($sql, $instance);
|
$sql = self::addPrefix($sql, $instance);
|
||||||
if ($data) {
|
if ($data) {
|
||||||
@@ -43,12 +49,12 @@ class DBInstance
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
*
|
* Summary of lastInsert
|
||||||
* @ctag DBInstance::lastInsert();
|
* @param string $instance
|
||||||
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
|
public static function lastInsert(string $instance = 'default')
|
||||||
public static function lastInsert($instance = 'default')
|
|
||||||
{
|
{
|
||||||
$con = Connections::getConnection($instance);
|
$con = Connections::getConnection($instance);
|
||||||
return $con->lastInsertId();
|
return $con->lastInsertId();
|
||||||
@@ -62,11 +68,11 @@ class DBInstance
|
|||||||
* @ctag DBInstance::query($sql);
|
* @ctag DBInstance::query($sql);
|
||||||
* @ctag DBInstance::query($sql, []);
|
* @ctag DBInstance::query($sql, []);
|
||||||
*
|
*
|
||||||
* @param String $sql The SQL to be executed
|
* @param string $sql The SQL to be executed
|
||||||
* @param Array $data Pass the info to be replaced on the query_prepare
|
* @param array $data Pass the info to be replaced on the query_prepare
|
||||||
* @param String $instance Select the connection to execute
|
* @param string $instance Select the connection to execute
|
||||||
*
|
*
|
||||||
* @return pointer A pointer for a foreach loop
|
* @return mixed A pointer for a foreach loop
|
||||||
*/
|
*/
|
||||||
public static function query($sql, $data = null, $instance = 'default')
|
public static function query($sql, $data = null, $instance = 'default')
|
||||||
{
|
{
|
||||||
@@ -75,8 +81,12 @@ class DBInstance
|
|||||||
if ($data) {
|
if ($data) {
|
||||||
return self::queryPrepare($sql, $data, $instance);
|
return self::queryPrepare($sql, $data, $instance);
|
||||||
} else {
|
} else {
|
||||||
|
try {
|
||||||
$con = Connections::getConnection($instance);
|
$con = Connections::getConnection($instance);
|
||||||
return $con->query($sql, \PDO::FETCH_OBJ);
|
return $con->query($sql, \PDO::FETCH_OBJ);
|
||||||
|
} catch (\Throwable $th) {
|
||||||
|
ddd($sql);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,13 +98,13 @@ class DBInstance
|
|||||||
*
|
*
|
||||||
* This method will execute a SQL and return the first featched register
|
* This method will execute a SQL and return the first featched register
|
||||||
*
|
*
|
||||||
* @param String $sql The SQL to be executed
|
* @param string $sql The SQL to be executed
|
||||||
* @param Array $data Pass the info to be replaced on the query_prepare
|
* @param array $data Pass the info to be replaced on the query_prepare
|
||||||
* @param String $instance Select the connection to execute
|
* @param string $instance Select the connection to execute
|
||||||
*
|
*
|
||||||
* @return stdClass A object with the record info
|
* @return \stdClass A object with the record info
|
||||||
*/
|
*/
|
||||||
public static function queryOne($sql, $data = null, $instance = 'default')
|
public static function queryOne(string $sql, Array $data = null, string $instance = 'default')
|
||||||
{
|
{
|
||||||
|
|
||||||
$sql = self::addPrefix($sql, $instance);
|
$sql = self::addPrefix($sql, $instance);
|
||||||
@@ -116,12 +126,12 @@ class DBInstance
|
|||||||
* @ctag DBInstance::getRecord($table, []);
|
* @ctag DBInstance::getRecord($table, []);
|
||||||
* @ctag DBInstance::getRecord($table, [], []);
|
* @ctag DBInstance::getRecord($table, [], []);
|
||||||
*
|
*
|
||||||
* @param String $table The table to be searched
|
* @param string $table The table to be searched
|
||||||
* @param Array $data Pass the info to be replaced on the query_prepare
|
* @param array $data Pass the info to be replaced on the query_prepare
|
||||||
* @param Array $select Info to be put on the SELECT part of the query
|
* @param array $select Info to be put on the SELECT part of the query
|
||||||
* @param String $instance Select the connection to execute
|
* @param string $instance Select the connection to execute
|
||||||
*
|
*
|
||||||
* @return stdClass A object with the record info
|
* @return mixed A object with the record info
|
||||||
*/
|
*/
|
||||||
public static function getRecord($table, $data = array(), $select = array('*'), $instance = 'default')
|
public static function getRecord($table, $data = array(), $select = array('*'), $instance = 'default')
|
||||||
{
|
{
|
||||||
@@ -167,11 +177,11 @@ class DBInstance
|
|||||||
* @ctag DBInstance::getRecordSql($sql);
|
* @ctag DBInstance::getRecordSql($sql);
|
||||||
* @ctag DBInstance::getRecordSql($sql, []);
|
* @ctag DBInstance::getRecordSql($sql, []);
|
||||||
*
|
*
|
||||||
* @param String $sql The SQL to be executed
|
* @param string $sql The SQL to be executed
|
||||||
* @param Array $data Pass the info to be replaced on the query_prepare
|
* @param array $data Pass the info to be replaced on the query_prepare
|
||||||
* @param String $instance Select the connection to execute
|
* @param string $instance Select the connection to execute
|
||||||
*
|
*
|
||||||
* @return stdClass A object with the record info
|
* @return mixed A object with the record info
|
||||||
*/
|
*/
|
||||||
public static function getRecordSql($sql, $data = null, $instance = 'default')
|
public static function getRecordSql($sql, $data = null, $instance = 'default')
|
||||||
{
|
{
|
||||||
@@ -193,12 +203,12 @@ class DBInstance
|
|||||||
* @ctag DBInstance::getRecords($table);
|
* @ctag DBInstance::getRecords($table);
|
||||||
* @ctag DBInstance::getRecords($table, []);
|
* @ctag DBInstance::getRecords($table, []);
|
||||||
*
|
*
|
||||||
* @param String $table The table to be searched
|
* @param string $table The table to be searched
|
||||||
* @param Array $data Pass the info to be replaced on the query_prepare
|
* @param array $data Pass the info to be replaced on the query_prepare
|
||||||
* @param Array $select Info to be put on the SELECT part of the query
|
* @param array $select Info to be put on the SELECT part of the query
|
||||||
* @param String $instance Select the connection to execute
|
* @param string $instance Select the connection to execute
|
||||||
*
|
*
|
||||||
* @return stdClass A object with the record info
|
* @return array A object with the record info
|
||||||
*/
|
*/
|
||||||
public static function getRecords($table, $data = array(), $select = array('*'), $limits = array(), $instance = 'default')
|
public static function getRecords($table, $data = array(), $select = array('*'), $limits = array(), $instance = 'default')
|
||||||
{
|
{
|
||||||
@@ -220,12 +230,12 @@ class DBInstance
|
|||||||
* @ctag DBInstance::getRecordsPointer($table);
|
* @ctag DBInstance::getRecordsPointer($table);
|
||||||
* @ctag DBInstance::getRecordsPointer($table, []);
|
* @ctag DBInstance::getRecordsPointer($table, []);
|
||||||
*
|
*
|
||||||
* @param String $table The table to be searched
|
* @param string $table The table to be searched
|
||||||
* @param Array $data Pass the info to be replaced on the query_prepare
|
* @param array $data Pass the info to be replaced on the query_prepare
|
||||||
* @param Array $select Info to be put on the SELECT part of the query
|
* @param array $select Info to be put on the SELECT part of the query
|
||||||
* @param String $instance Select the connection to execute
|
* @param string $instance Select the connection to execute
|
||||||
*
|
*
|
||||||
* @return stdClass A object with the record info
|
* @return mixed A object with the record info
|
||||||
*/
|
*/
|
||||||
public static function getRecordsPointer($table, $data = array(), $select = array('*'), $limits = array(), $instance = 'default')
|
public static function getRecordsPointer($table, $data = array(), $select = array('*'), $limits = array(), $instance = 'default')
|
||||||
{
|
{
|
||||||
@@ -277,11 +287,11 @@ class DBInstance
|
|||||||
* @ctag DBInstance::getRecordsSql($sql);
|
* @ctag DBInstance::getRecordsSql($sql);
|
||||||
* @ctag DBInstance::getRecordsSql($sql, []);
|
* @ctag DBInstance::getRecordsSql($sql, []);
|
||||||
*
|
*
|
||||||
* @param String $sql The SQL to be executed
|
* @param string $sql The SQL to be executed
|
||||||
* @param Array $data Pass the info to be replaced on the query_prepare
|
* @param array $data Pass the info to be replaced on the query_prepare
|
||||||
* @param String $instance Select the connection to execute
|
* @param string $instance Select the connection to execute
|
||||||
*
|
*
|
||||||
* @return Array Array with objects representing the database data
|
* @return array Array with objects representing the database data
|
||||||
*/
|
*/
|
||||||
public static function getRecordsSql($sql, $data = null, $instance = 'default')
|
public static function getRecordsSql($sql, $data = null, $instance = 'default')
|
||||||
{
|
{
|
||||||
@@ -302,6 +312,13 @@ class DBInstance
|
|||||||
return $elements;
|
return $elements;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Summary of insertRecord
|
||||||
|
* @param string $table
|
||||||
|
* @param array $data
|
||||||
|
* @param string $connection
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
public static function insertRecord($table, $data = array(), $connection = 'default')
|
public static function insertRecord($table, $data = array(), $connection = 'default')
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -330,12 +347,24 @@ class DBInstance
|
|||||||
DBInstance::execute($sql, $insert_data, $connection);
|
DBInstance::execute($sql, $insert_data, $connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Summary of queryPrepare
|
||||||
|
* @param string $sql
|
||||||
|
* @param array $data
|
||||||
|
* @param string $instance
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
public static function queryPrepare($sql, $data = array(), $instance = 'default')
|
public static function queryPrepare($sql, $data = array(), $instance = 'default')
|
||||||
{
|
{
|
||||||
|
try {
|
||||||
$sql = self::addPrefix($sql, $instance);
|
$sql = self::addPrefix($sql, $instance);
|
||||||
$con = Connections::getConnection($instance);
|
$con = Connections::getConnection($instance);
|
||||||
$statement = $con->prepare($sql);
|
$statement = $con->prepare($sql);
|
||||||
$statement->execute($data);
|
$statement->execute($data);
|
||||||
return $statement->fetchAll(\PDO::FETCH_OBJ);
|
return $statement->fetchAll(\PDO::FETCH_OBJ);
|
||||||
|
} catch (\Throwable $th) {
|
||||||
|
error_log('adsdsadsadsa');
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+16
-9
@@ -30,7 +30,7 @@ abstract class Entity {
|
|||||||
|
|
||||||
function __construct($assignment = '') {
|
function __construct($assignment = '') {
|
||||||
|
|
||||||
@$this->_ignore = array_merge($this->_ignore, self::_ignore);
|
@$this->_ignore = array_merge((array) $this->_ignore, self::_ignore);
|
||||||
|
|
||||||
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');
|
||||||
@@ -51,9 +51,17 @@ abstract class Entity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function __get($key) {
|
public function __get($key) {
|
||||||
|
// The __get might be a connection. So return it
|
||||||
|
if(method_exists($this, $key)){
|
||||||
|
return $this->$key();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Empty is bad for this kind of dynamicity
|
||||||
if (!isset($this->$key)) {
|
if (!isset($this->$key)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// There is a key. So return it
|
||||||
return $this->$key;
|
return $this->$key;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -238,7 +246,6 @@ abstract class Entity {
|
|||||||
foreach ($data as $key => $value) {
|
foreach ($data as $key => $value) {
|
||||||
$this->$key = $value;
|
$this->$key = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -405,9 +412,9 @@ abstract class Entity {
|
|||||||
/**
|
/**
|
||||||
* Find Many
|
* Find Many
|
||||||
*
|
*
|
||||||
* @param Array $criterias Description
|
* @param array $criterias Description
|
||||||
* @param Array $select Description
|
* @param array $select Description
|
||||||
* @param Array $limits Description
|
* @param array $limits Description
|
||||||
* @param boolean $trashed Description
|
* @param boolean $trashed Description
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@@ -490,10 +497,10 @@ abstract class Entity {
|
|||||||
*
|
*
|
||||||
* @ctag $this->hasOne('foreign_object', 'field_in_remote');
|
* @ctag $this->hasOne('foreign_object', 'field_in_remote');
|
||||||
*
|
*
|
||||||
* @param Object $foreign_object Class instance from the remote object
|
* @param object $foreign_object Class instance from the remote object
|
||||||
* @param (int, string) $field_in_remote Field in local object matchin the remote id
|
* @param (int, string) $field_in_remote Field in local object matchin the remote id
|
||||||
*
|
*
|
||||||
* @return Object Instance of the remote class
|
* @return object Instance of the remote class
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
protected function hasOne($foreign_object, $field_in_remote) {
|
protected function hasOne($foreign_object, $field_in_remote) {
|
||||||
@@ -513,7 +520,7 @@ abstract class Entity {
|
|||||||
*
|
*
|
||||||
* @ctag $this->hasMany('foreign_object', 'field_in_foreign');
|
* @ctag $this->hasMany('foreign_object', 'field_in_foreign');
|
||||||
*
|
*
|
||||||
* @param type $foreign_object Instance of a remote class
|
* @param Entity $foreign_object Instance of a remote class
|
||||||
* @param (int, string) $field_in_foreign The field to match the local id
|
* @param (int, string) $field_in_foreign The field to match the local id
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@@ -536,7 +543,7 @@ abstract class Entity {
|
|||||||
* @ctag $this->belongsTo('foreign_object', 'local_field');
|
* @ctag $this->belongsTo('foreign_object', 'local_field');
|
||||||
* @ctag $this->belongsTo('foreign_object', 'local_field', 'id');
|
* @ctag $this->belongsTo('foreign_object', 'local_field', 'id');
|
||||||
*
|
*
|
||||||
* @param Object $foreign_object Instance of a remote class
|
* @param Entity $foreign_object Instance of a remote class
|
||||||
* @param (int, String) $local_field Remote field relate to local Object
|
* @param (int, String) $local_field Remote field relate to local Object
|
||||||
* @param string $remote_field
|
* @param string $remote_field
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user