This commit is contained in:
2023-04-25 18:09:41 +00:00
parent 7b2f03e139
commit c2f00d6c21
6 changed files with 118 additions and 74 deletions
+10
View File
@@ -16,6 +16,16 @@ class BelongsTo {
$this->localElement = $localElement;
}
public function __call($name, $arguments)
{
return $this->get()->$name($arguments);
}
public function __get($key)
{
return $this->get()->$key;
}
function get() {
return $this->foreignObject::findOne(Array($this->remoteField => Array('=', $this->localElement->{$this->localField})));
}
+2 -2
View File
@@ -22,7 +22,7 @@ class BelongsToMany {
$this->remoteLimit = $remoteLimit;
}
function get() {
function get() : Collection {
$limitsSql = '';
foreach ($this->remoteLimit as $key => $value) {
@@ -40,7 +40,7 @@ class BelongsToMany {
}
if (empty($ids)) {
return Array();
return new Collection();
}
return $this->foreignObject::findMany(array_merge(Array('id' => Array('IN', $ids)), $this->remoteFilter), Array('*'), $this->remoteLimit);
+4 -4
View File
@@ -24,7 +24,7 @@ class BelongsToManyExtended {
$this->softDelete = $softDelete;
}
function get(){
function get() : Collection {
$limitsSql = '';
@@ -42,10 +42,10 @@ class BelongsToManyExtended {
}
$relations = DBInstance::query($sql, Array($this->localObject->id), $this->localObject::_connectionName);
$objects = Array();
$objects = new Collection();
if (empty($relations)) {
return Array();
return new Collection();
}
foreach ($relations as $relation) {
@@ -54,7 +54,7 @@ class BelongsToManyExtended {
}
if (empty($objects)) {
return Array();
return new Collection();
}
return $objects;
+3 -5
View File
@@ -26,13 +26,11 @@ class Collection implements \Countable, \IteratorAggregate, \ArrayAccess
}
// https://www.php.net/manual/en/class.iteratoraggregate.php
public function getIterator()
public function getIterator(): \Generator
{
return (function () {
while (list($key, $val) = each($this->items)) {
foreach ($this->items as $key => $val) {
yield $key => $val;
}
})();
}
// 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()
+74 -45
View File
@@ -2,10 +2,19 @@
namespace ORM;
/**
* Summary of 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);
@@ -25,14 +34,11 @@ class DBInstance
*
* This method will execute a SQL on the database
*
* @ctag DBInstance::execute($sql);
* @ctag DBInstance::execute($sql, []);
*
* @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
* @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);
if ($data) {
@@ -43,12 +49,12 @@ class DBInstance
}
}
/*
*
* @ctag DBInstance::lastInsert();
/**
* Summary of lastInsert
* @param string $instance
* @return mixed
*/
public static function lastInsert($instance = 'default')
public static function lastInsert(string $instance = 'default')
{
$con = Connections::getConnection($instance);
return $con->lastInsertId();
@@ -62,11 +68,11 @@ class DBInstance
* @ctag DBInstance::query($sql);
* @ctag DBInstance::query($sql, []);
*
* @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
* @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
*
* @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')
{
@@ -75,8 +81,12 @@ class DBInstance
if ($data) {
return self::queryPrepare($sql, $data, $instance);
} else {
try {
$con = Connections::getConnection($instance);
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
*
* @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
* @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
*
* @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);
@@ -116,12 +126,12 @@ class DBInstance
* @ctag DBInstance::getRecord($table, []);
* @ctag DBInstance::getRecord($table, [], []);
*
* @param String $table The table to be searched
* @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 String $instance Select the connection to execute
* @param string $table The table to be searched
* @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 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')
{
@@ -167,11 +177,11 @@ class DBInstance
* @ctag DBInstance::getRecordSql($sql);
* @ctag DBInstance::getRecordSql($sql, []);
*
* @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
* @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
*
* @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')
{
@@ -193,12 +203,12 @@ class DBInstance
* @ctag DBInstance::getRecords($table);
* @ctag DBInstance::getRecords($table, []);
*
* @param String $table The table to be searched
* @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 String $instance Select the connection to execute
* @param string $table The table to be searched
* @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 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')
{
@@ -220,12 +230,12 @@ class DBInstance
* @ctag DBInstance::getRecordsPointer($table);
* @ctag DBInstance::getRecordsPointer($table, []);
*
* @param String $table The table to be searched
* @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 String $instance Select the connection to execute
* @param string $table The table to be searched
* @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 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')
{
@@ -277,11 +287,11 @@ class DBInstance
* @ctag DBInstance::getRecordsSql($sql);
* @ctag DBInstance::getRecordsSql($sql, []);
*
* @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
* @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
*
* @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')
{
@@ -302,6 +312,13 @@ class DBInstance
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')
{
@@ -330,12 +347,24 @@ class DBInstance
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')
{
try {
$sql = self::addPrefix($sql, $instance);
$con = Connections::getConnection($instance);
$statement = $con->prepare($sql);
$statement->execute($data);
return $statement->fetchAll(\PDO::FETCH_OBJ);
} catch (\Throwable $th) {
error_log('adsdsadsadsa');
}
}
}
+16 -9
View File
@@ -30,7 +30,7 @@ abstract class Entity {
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) {
$this->created_at = date('Y-m-d h:m:s');
@@ -51,9 +51,17 @@ abstract class Entity {
}
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)) {
return 0;
}
// There is a key. So return it
return $this->$key;
}
@@ -238,7 +246,6 @@ abstract class Entity {
foreach ($data as $key => $value) {
$this->$key = $value;
}
}
/**
@@ -405,9 +412,9 @@ abstract class Entity {
/**
* Find Many
*
* @param Array $criterias Description
* @param Array $select Description
* @param Array $limits Description
* @param array $criterias Description
* @param array $select Description
* @param array $limits Description
* @param boolean $trashed Description
*
*/
@@ -490,10 +497,10 @@ abstract class Entity {
*
* @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
*
* @return Object Instance of the remote class
* @return object Instance of the remote class
*
*/
protected function hasOne($foreign_object, $field_in_remote) {
@@ -513,7 +520,7 @@ abstract class Entity {
*
* @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
*
*/
@@ -536,7 +543,7 @@ abstract class Entity {
* @ctag $this->belongsTo('foreign_object', 'local_field');
* @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 string $remote_field
*/