Some identation, documentation and minor changes
This commit is contained in:
+136
-93
@@ -2,12 +2,19 @@
|
||||
|
||||
namespace ORM;
|
||||
|
||||
abstract class Entity {
|
||||
use ORM\Relations\HasOne;
|
||||
use ORM\Relations\HasMany;
|
||||
use ORM\Relations\BelongsTo;
|
||||
use ORM\Relations\BelongsToMany;
|
||||
use ORM\Relations\BelongsToManyExtended;
|
||||
|
||||
abstract class Entity
|
||||
{
|
||||
|
||||
const _tableName = '';
|
||||
const _connectionName = 'default';
|
||||
const _softdelete = false;
|
||||
const _ignore = Array(
|
||||
const _ignore = array(
|
||||
'classname',
|
||||
'_properties',
|
||||
'_ignore',
|
||||
@@ -18,7 +25,7 @@ abstract class Entity {
|
||||
'_idPolice',
|
||||
'_ignore'
|
||||
);
|
||||
const _properties = Array();
|
||||
const _properties = array();
|
||||
|
||||
/*
|
||||
* [type] = nextval| manual | empty is the default
|
||||
@@ -26,9 +33,10 @@ abstract class Entity {
|
||||
* [min] = minumul value allowed
|
||||
* [step] = increment by n each id
|
||||
*/
|
||||
const _idPolice = Array();
|
||||
const _idPolice = array();
|
||||
|
||||
function __construct($assignment = '') {
|
||||
function __construct($assignment = '')
|
||||
{
|
||||
|
||||
@$this->_ignore = array_merge((array) $this->_ignore, self::_ignore);
|
||||
|
||||
@@ -43,14 +51,16 @@ abstract class Entity {
|
||||
}
|
||||
}
|
||||
|
||||
public function __set($property, $value) {
|
||||
public function __set($property, $value)
|
||||
{
|
||||
if (in_array($property, static::_ignore)) {
|
||||
return;
|
||||
}
|
||||
$this->$property = $value;
|
||||
}
|
||||
|
||||
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();
|
||||
@@ -65,7 +75,8 @@ abstract class Entity {
|
||||
return $this->$key;
|
||||
}
|
||||
|
||||
public function __toString() {
|
||||
public function __toString()
|
||||
{
|
||||
if (isset($this->name)) {
|
||||
return $this->name;
|
||||
}
|
||||
@@ -75,11 +86,10 @@ abstract class Entity {
|
||||
return static::class;
|
||||
}
|
||||
|
||||
/*public function __call($name, $arguments){
|
||||
dd($name);
|
||||
}*/
|
||||
// public function __call($name, $arguments){ }
|
||||
|
||||
public function charge($payload) {
|
||||
public function charge($payload)
|
||||
{
|
||||
|
||||
$properties = [];
|
||||
|
||||
@@ -103,8 +113,9 @@ abstract class Entity {
|
||||
return [];
|
||||
}
|
||||
|
||||
public static function get_properties() {
|
||||
$properties = Array();
|
||||
public static function get_properties()
|
||||
{
|
||||
$properties = array();
|
||||
foreach (static::_properties as $propertie) {
|
||||
$obj = new \stdClass();
|
||||
$obj->data = $propertie;
|
||||
@@ -113,7 +124,8 @@ abstract class Entity {
|
||||
return json_encode($properties);
|
||||
}
|
||||
|
||||
public function save() {
|
||||
public function save()
|
||||
{
|
||||
if (isset($this->id)) {
|
||||
$this->update();
|
||||
} else {
|
||||
@@ -121,9 +133,10 @@ abstract class Entity {
|
||||
}
|
||||
}
|
||||
|
||||
private function update() {
|
||||
private function update()
|
||||
{
|
||||
//First check if the record exist. If not, we might have a idPolice
|
||||
if (!DBInstance::queryOne("SELECT id FROM {" . static::_tableName . "} WHERE id = ?", Array($this->id))) {
|
||||
if (!DBInstance::queryOne("SELECT id FROM {" . static::_tableName . "} WHERE id = ?", array($this->id))) {
|
||||
$this->create();
|
||||
return;
|
||||
}
|
||||
@@ -134,7 +147,7 @@ abstract class Entity {
|
||||
$obj = (array) $this;
|
||||
|
||||
$update_string = '';
|
||||
$update_data = Array();
|
||||
$update_data = array();
|
||||
$update_data['_update_id'] = $this->id;
|
||||
|
||||
if (isset($this->_timestamps) && $this->_timestamps) {
|
||||
@@ -152,7 +165,8 @@ abstract class Entity {
|
||||
DBInstance::execute($sql, $update_data, static::_connectionName);
|
||||
}
|
||||
|
||||
private function create() {
|
||||
private function create()
|
||||
{
|
||||
//First, let's verify id polices
|
||||
//Manual and empty don't need special treatments
|
||||
if (!empty(static::_idPolice) && static::_idPolice['type'] != 'manual') {
|
||||
@@ -185,7 +199,7 @@ abstract class Entity {
|
||||
|
||||
$first_argument = '';
|
||||
$second_argument = '';
|
||||
$insert_data = Array();
|
||||
$insert_data = array();
|
||||
|
||||
foreach ($columns as $column) {
|
||||
$first_argument .= $column . ', ';
|
||||
@@ -207,17 +221,19 @@ abstract class Entity {
|
||||
}
|
||||
|
||||
|
||||
public function load($id = false) {
|
||||
public function load($id = false)
|
||||
{
|
||||
if (!$id) {
|
||||
return;
|
||||
}
|
||||
|
||||
$sql = "SELECT * FROM {" . static::_tableName . "} WHERE id = ?";
|
||||
|
||||
$this->fill(DBInstance::queryPrepare($sql, Array($id), static::_connectionName)[0]);
|
||||
$this->fill(DBInstance::queryPrepare($sql, array($id), static::_connectionName)[0]);
|
||||
}
|
||||
|
||||
public function delete() {
|
||||
public function delete()
|
||||
{
|
||||
if (static::_softdelete) {
|
||||
$this->deleted_at = date('Y-m-d h:m:s');
|
||||
$this->update();
|
||||
@@ -226,12 +242,14 @@ abstract class Entity {
|
||||
}
|
||||
}
|
||||
|
||||
public function purge() {
|
||||
public function purge()
|
||||
{
|
||||
$sql = "DELETE FROM {" . static::_tableName . "} WHERE id = :id";
|
||||
DBInstance::execute($sql, Array('id' => $this->id), static::_connectionName);
|
||||
DBInstance::execute($sql, array('id' => $this->id), static::_connectionName);
|
||||
}
|
||||
|
||||
public function restore(){
|
||||
public function restore()
|
||||
{
|
||||
if (isset($this->_softdelete) && $this->_softdelete) {
|
||||
$this->deleted_at = null;
|
||||
$this->update();
|
||||
@@ -240,7 +258,8 @@ abstract class Entity {
|
||||
}
|
||||
}
|
||||
|
||||
private function fill($data) {
|
||||
private function fill($data)
|
||||
{
|
||||
$data = (array) $data;
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
@@ -258,7 +277,8 @@ abstract class Entity {
|
||||
* @param Array $limits Array( 'offset'=> 10, 'limit' => 10 )
|
||||
* @param boolean $trashed Bring trashed elements?
|
||||
*/
|
||||
public static function findAll($select = Array('*'), $limits = Array(), $trashed = false) : Collection {
|
||||
public static function findAll($select = array('*'), $limits = array(), $trashed = false): Collection
|
||||
{
|
||||
$criteria = '';
|
||||
$limits_sql = '';
|
||||
|
||||
@@ -278,7 +298,7 @@ abstract class Entity {
|
||||
$sql = "SELECT $criteria FROM {" . static::_tableName . "} $limits_sql";
|
||||
}
|
||||
|
||||
$results = DBInstance::query($sql, Array(), static::_connectionName);
|
||||
$results = DBInstance::query($sql, array(), static::_connectionName);
|
||||
$objects = new Collection;
|
||||
|
||||
foreach ($results as $value) {
|
||||
@@ -300,9 +320,10 @@ abstract class Entity {
|
||||
* @param boolean $trashed Bring trashed registers?
|
||||
*
|
||||
*/
|
||||
public static function count($criterias = Array(), $trashed = false) {
|
||||
public static function count($criterias = array(), $trashed = false)
|
||||
{
|
||||
$criteria_sql = "";
|
||||
$criteria_data = Array();
|
||||
$criteria_data = array();
|
||||
foreach ($criterias as $key => $criteria) {
|
||||
if ($criteria_sql != "") {
|
||||
$criteria_sql .= " AND ";
|
||||
@@ -335,26 +356,31 @@ abstract class Entity {
|
||||
/**
|
||||
* Find One
|
||||
*
|
||||
* @param Array/Int $criterias Array('id' => Array('in', Array(10, 20, 30)))
|
||||
* @param Int|String|Array $criterias Array('id' => Array('in', Array(10, 20, 30)))
|
||||
* @param Array $select Array(id, fullname)
|
||||
* @param Boolan $trashed true, false
|
||||
* @param bool $trashed true, false
|
||||
*/
|
||||
public static function findOne($criterias = Array(), $select = Array('*'), $trashed = false) {
|
||||
public static function findOne(
|
||||
int|string|array $criterias = array(),
|
||||
array $select = array('*'),
|
||||
bool $trashed = false
|
||||
) {
|
||||
if (empty($criterias)) {
|
||||
$criterias = Array(1 => ['=', 1]);
|
||||
$criterias = array(1 => ['=', 1]);
|
||||
}
|
||||
|
||||
$select_sql = "";
|
||||
$criteria_sql = "";
|
||||
$limits_sql = "";
|
||||
$criteria_data = Array();
|
||||
$criteria_data = array();
|
||||
|
||||
$limits = Array("LIMIT" => 1);
|
||||
$limits = array("LIMIT" => 1);
|
||||
|
||||
if (is_numeric($criterias)) {
|
||||
if (!is_array($criterias)) {
|
||||
$criteria_sql = "WHERE id = ? ";
|
||||
$criteria_data[] = $criterias;
|
||||
} else {
|
||||
if (!array_is_list($criterias)) {
|
||||
foreach ($criterias as $key => $criteria) {
|
||||
if ($criteria_sql != "") {
|
||||
$criteria_sql .= " AND ";
|
||||
@@ -376,7 +402,12 @@ abstract class Entity {
|
||||
$criteria_data[] = $criteria[1];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$criteria_sql = "WHERE {$criterias[0]} = ? ";
|
||||
$criteria_data[] = $criterias[1];
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($select as $value) {
|
||||
$select_sql .= $value . ', ';
|
||||
}
|
||||
@@ -418,10 +449,13 @@ abstract class Entity {
|
||||
* @param boolean $trashed Description
|
||||
*
|
||||
*/
|
||||
public static function findMany($criterias = Array(), $select = Array('*'), $limits = Array(), $trashed = false) {
|
||||
$select_sql = "";
|
||||
$criteria_sql = "";
|
||||
$limits_sql = "";
|
||||
public static function findMany(
|
||||
$criterias = array(),
|
||||
$select = array('*'),
|
||||
$limits = array(),
|
||||
$trashed = false
|
||||
) {
|
||||
$select_sql = $criteria_sql = $limits_sql = "";
|
||||
|
||||
foreach ($limits as $key => $value) {
|
||||
$limits_sql .= "$key $value ";
|
||||
@@ -466,7 +500,7 @@ abstract class Entity {
|
||||
$sql = "SELECT $select_sql FROM {" . static::_tableName . "} $criteria_sql $limits_sql";
|
||||
}
|
||||
|
||||
$results = DBInstance::query($sql, Array(), static::_connectionName);
|
||||
$results = DBInstance::query($sql, array(), static::_connectionName);
|
||||
$objects = new Collection();
|
||||
|
||||
foreach ($results as $value) {
|
||||
@@ -477,14 +511,14 @@ abstract class Entity {
|
||||
}
|
||||
|
||||
if (empty($objects)) {
|
||||
return Array();
|
||||
return array();
|
||||
}
|
||||
|
||||
return $objects;
|
||||
}
|
||||
|
||||
/**
|
||||
* Has One Local
|
||||
* Has One
|
||||
*
|
||||
* Defines that, this object has a child an only one,
|
||||
* object in other class.
|
||||
@@ -495,15 +529,14 @@ abstract class Entity {
|
||||
* This relation will be created on the User class
|
||||
* making reference to the Phone class
|
||||
*
|
||||
* @ctag $this->hasOne('foreign_object', 'field_in_remote');
|
||||
*
|
||||
* @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 hasOne($foreign_object, $field_in_remote) {
|
||||
protected function hasOne($foreign_object, $field_in_remote)
|
||||
{
|
||||
return new HasOne($foreign_object, $field_in_remote, $this);
|
||||
}
|
||||
|
||||
@@ -518,14 +551,13 @@ abstract class Entity {
|
||||
* This relation will be created on the Post class
|
||||
* Making reference to the Comment class
|
||||
*
|
||||
* @ctag $this->hasMany('foreign_object', 'field_in_foreign');
|
||||
*
|
||||
* @param Entity $foreign_object Instance of a remote class
|
||||
* @param (int, string) $field_in_foreign The field to match the local id
|
||||
* @param string $foreignObject Instance of a remote class
|
||||
* @param int|string $fieldInForeign The field to match the local id
|
||||
*
|
||||
*/
|
||||
protected function hasMany($foreign_object, $field_in_foreign) {
|
||||
return new HasMany($foreign_object, $field_in_foreign, $this);
|
||||
protected function hasMany(string $foreignObject, int|string $fieldInForeign)
|
||||
{
|
||||
return new HasMany($foreignObject, $fieldInForeign, $this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -540,14 +572,12 @@ abstract class Entity {
|
||||
* This relation will be created on the comment class
|
||||
* Making reference to the post class
|
||||
*
|
||||
* @ctag $this->belongsTo('foreign_object', 'local_field');
|
||||
* @ctag $this->belongsTo('foreign_object', 'local_field', 'id');
|
||||
*
|
||||
* @param Entity $foreign_object Instance of a remote class
|
||||
* @param (int, String) $local_field Remote field relate to local Object
|
||||
* @param string $remote_field
|
||||
* @param string $foreignObject Instance of a remote class
|
||||
* @param (int, String) $localField Remote field relate to local Object
|
||||
* @param string $remoteField
|
||||
*/
|
||||
protected function belongsTo($foreignObject, $localField, $remoteField = 'id') {
|
||||
protected function belongsTo(string $foreignObject, $localField, $remoteField = 'id')
|
||||
{
|
||||
return new BelongsTo($foreignObject, $localField, $remoteField, $this);
|
||||
}
|
||||
|
||||
@@ -563,19 +593,22 @@ abstract class Entity {
|
||||
*
|
||||
* This relation will be created in both classes
|
||||
*
|
||||
* @ctag $this->belongsToMany('foreign_object', 'pivot_table', 'local_in_pivot', 'remote_in_pivot');
|
||||
* @ctag $this->belongsToMany('foreign_object', 'pivot_table', 'local_in_pivot', 'remote_in_pivot', $remote_filter = Array());
|
||||
* @ctag $this->belongsToMany('foreign_object', 'pivot_table', 'local_in_pivot', 'remote_in_pivot', $remote_filter = Array(), $remote_limit = Array());
|
||||
*
|
||||
* @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 )
|
||||
* @param Object $foreignObject Instance of the remote class
|
||||
* @param string $pivotTable Name of the pivot table
|
||||
* @param string $localInPivot Name of field on the pivot in relation of the local class
|
||||
* @param int|string $remoteInPivot Name of field on the pivot in relation of the remote class
|
||||
* @param Array $remoteFilter Filters to the remote Array('id', Array('>', 50) )
|
||||
* @param Array $remoteLimit Array( 'offset'=> 10, 'limit' => 10 )
|
||||
*/
|
||||
protected function belongsToMany($foreignObject, $pivot_table, $local_in_pivot, $remote_in_pivot, $remote_filter = Array(), $remote_limit = Array()) {
|
||||
return new BelongsToMany($this, $foreignObject, $pivot_table, $local_in_pivot, $remote_in_pivot, $remote_filter, $remote_limit);
|
||||
protected function belongsToMany(
|
||||
string $foreignObject,
|
||||
string $pivotTable,
|
||||
string $localInPivot,
|
||||
int|string $remoteInPivot,
|
||||
array $remoteFilter = array(),
|
||||
array $remoteLimit = array()
|
||||
) {
|
||||
return new BelongsToMany($this, $foreignObject, $pivotTable, $localInPivot, $remoteInPivot, $remoteFilter, $remoteLimit);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -589,38 +622,48 @@ abstract class Entity {
|
||||
* Table Tag = (id, name)
|
||||
* Table Post_Tag = (id, postid, tagid)
|
||||
*
|
||||
* @ctag $this->belongsToManyExtended('foreign_object', 'pivot_table', 'local_in_pivot', 'remote_in_pivot');
|
||||
* @ctag $this->belongsToManyExtended('foreign_object', 'pivot_table', 'local_in_pivot', 'remote_in_pivot', $remote_filter = Array());
|
||||
* @ctag $this->belongsToManyExtended('foreign_object', 'pivot_table', 'local_in_pivot', 'remote_in_pivot', $remote_filter = Array(), $pivot_limits = Array());
|
||||
*
|
||||
* 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 )
|
||||
* @param Object $foreignObject Instance of the remote class
|
||||
* @param String $pivotTable Name of the pivot table
|
||||
* @param String $localInPivot Name of field on the pivot in relation of the local class
|
||||
* @param String $remoteInPivot Name of field on the pivot in relation of the remote class
|
||||
* @param Array $remoteFilter Filters to the remote Array('id', Array('>', 50) )
|
||||
* @param Array $pivotLimit Array( 'offset'=> 10, 'limit' => 10 )
|
||||
* @param bool $softDelete
|
||||
*/
|
||||
protected function belongsToManyExtended($foreignObject, $pivot_table, $local_in_pivot, $remote_in_pivot, $remote_filter = Array(), $pivot_limit = Array(), $softDelete = true) {
|
||||
return new BelongsToManyExtended($this, $foreignObject, $pivot_table, $local_in_pivot, $remote_in_pivot, $remote_filter, $pivot_limit, $softDelete);
|
||||
protected function belongsToManyExtended(
|
||||
string $foreignObject,
|
||||
string $pivotTable,
|
||||
string $localInPivot,
|
||||
string $remoteInPivot,
|
||||
array $remoteFilter = array(),
|
||||
array $pivotLimit = array(),
|
||||
$softDelete = true
|
||||
) {
|
||||
return new BelongsToManyExtended($this, $foreignObject, $pivotTable, $localInPivot, $remoteInPivot, $remoteFilter, $pivotLimit, $softDelete);
|
||||
}
|
||||
|
||||
/**
|
||||
* With
|
||||
*
|
||||
* Eager load relations on this entity
|
||||
*
|
||||
**/
|
||||
public function with($relations = null, $propagate = null){
|
||||
if(is_string($relations)){
|
||||
$relations = [$relations];
|
||||
}
|
||||
|
||||
* @param null|array|string $relations
|
||||
* @param null|array|string $propagate
|
||||
*
|
||||
* @return Entity
|
||||
*/
|
||||
public function with(
|
||||
null|array|string $relations = null,
|
||||
null|array|string $propagate = null
|
||||
) {
|
||||
if (!$relations) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
if (is_string($relations)) {
|
||||
$relations = [$relations];
|
||||
}
|
||||
|
||||
$possibleRelations = get_class_methods(static::class);
|
||||
|
||||
foreach ($relations as $relation) {
|
||||
|
||||
Reference in New Issue
Block a user