Skip to content

Commit

Permalink
modified [Models] logic
Browse files Browse the repository at this point in the history
  • Loading branch information
isocroft committed Feb 10, 2017
1 parent 1a55302 commit 6cc23d5
Show file tree
Hide file tree
Showing 10 changed files with 662 additions and 68 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Thumbs.db

# skip base files/folders

_temp
hooks
packages/vendor
packages/node_modules
Expand Down
264 changes: 252 additions & 12 deletions models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
*/

use \Contracts\Policies\DBAccessInterface as DBInterface;
use \Providers\Core\QueryBuilder as Builder;


class Model implements DBInterface {

public static $class = NULL;

protected static $instance = NULL;

protected $table = 'NULL';

protected $primaryKey = 'NULL';
Expand All @@ -25,54 +27,292 @@ class Model implements DBInterface {
);

public function __construct(){

static::setInstance($this);

}

$envfile = $env['app.path.base'] . '.env';

if(file_exists($envfile)){
public function __destruct(){

$app->setDBConnection($envfile);
static::unsetInstance();
}

}else{
protected static function unsetInstance(){

throw new \Exception("Cannot create Model Instance >> Database Settings Not Found");
if(static::$instance){

}
static::$instance = NULL;
}
}

protected function setBuilder(Builder $builder){
protected static function setInstance(Model $m){

/*
* A trick to instantiate an class without calling the constructor
* > credits: PHPUnit Framework Project - GitHub
*
* -- This trick has been modified for use in Jollof
*/

if(!isset(static::$instance)){

$m->builder = $app->getBuilder($m->getAttributes());

static::$instance = unserialize(
sprintf('O:%d:"%s":0:{}', strlen(get_called_class()), get_class($m))
);

static::$class = get_class(static::$instance);

static::$instance->builder = $m->builder;

}else{

$m->builder = static::$instance->builder;

}

}

$this->builder = $builder;
}

/**
*
*
*
*
* @param
* @return
*/

protected function rawGet(array $columns = array(), array $clauseProps = array(), $conjunction = 'and'){
;
}


/**
*
*
*
*
* @param array $columns
* @param array $clauseProps
* @param string $conjunction
* @return Providers\Core\QueryExtender
*/

protected function get(array $columns = array(), array $clauseProps = array(), $conjunction = 'and'){

return $this->builder->select($columns, $clauseProps, $conjunction);
}


/**
*
*
*
*
* @param array $values
* @param array $clauseProps
* @return Providers\Core\QueryExtender
*/

protected function set(array $values = array(), array $clauseProps = array()){

return $this->builder->insert($values, $clauseProps);
}


/**
*
*
*
*
* @param array $columnValues
* @param array $clauseProps
* @param string $conjunction
* @return Providers\Core\QueryExtender
*/

protected function let(array $columnValues = array(), $clauseProps = array(), $conjunction = 'and'){

return $this->builder->update($columnValues, $clauseProps, $conjunction);
}


/**
*
*
*
*
* @param array $columns
* @param array $clauseProps
* @return \Providers\Core\QueryExtender
*/

protected function del(array $columns = array(), $clauseProps = array()){

return $this->builder->delete($columns, $clauseProps);
}


/**
*
*
*
*
* @param void
* @return array
*/

public function getAttributes(){

return array('table' => $this->table, 'key' => $this->primaryKey, 'relations' => $this->relations);
}


/**
*
*
*
*
* @param void
* @return void
*/

public function bindSchema(){

;
}

/**
* Retrieves one or more tuples/rows from a Model table
* based on conditions.
*
*
* @param void
* @return void
*/

public static function whereBy(array $clause){

return static::$instance->get(array('*'), $clause)->exec();
}

/**
*
*
*
*
* @param
* @return
*/

public static function fetchAllWith($modelName, array $clause){

return static::$instance->get(array('*'), $clause)->with($modelName)->exec();
}

/**
* Updates a tuple/row from the Model table using the
* value of the tables' primary key {$id}
*
*
* @param string $id
* @return array
* @api
*/

public static function updateById($id = ''){

$attr = static::$instance->getAttributes();
$clause = array();
$clause[$attr['key']] = $id;

return static::$instance->let(array('*'), $clause)->exec(0);
}

/**
* Deletes a tuple/row from the Model table using the
* value of the tables' primary key {$id}
*
*
* @param string $id
* @return array
* @api
*/

public static function removeById($id = ''){

$attr = static::$instance->getAttributes();
$clause = array();
$clause[$attr['key']] = $id;

return static::$instance->del(array('*'), $clause)->exec(0);
}

/**
* Retrieves a tuple/row from the Model table using the
* value of the tables' primary key {$id}
*
*
* @param string $id
* @return array
* @api
*/

public static function findById($id = ''){

$attr = static::$instance->getAttributes();
$clause = array();
$clause[$attr['key']] = $id;

return static::$instance->get(array('*'), $clause)->exec();
}

/**
* Retrieves all tuples/rows from the Model table
*
*
*
* @param integer $limit
* @param integer $offset
* @return array
* @api
*/

public static function fetchAll($limit = -1, $offset = -1){

return static::$instance->get(array('*'))->exec($limit, $offset);
}

/**
* Inserts OR Updates a tuple/row in the Model table
*
*
*
* @param array $tuple
* @return array
*/

public static function createOrUpdate(array $tuple, array $clause){

return static::$instance->set($tuple, $clause)->exec(0);
}

/**
* Inserts a tuple/row into the Model table
*
*
*
* @param array $tuple
* @return array
*/

public static function create(array $tuple){

return static::$instance->set($tuple)->exec(0);
}

}


Expand Down
2 changes: 1 addition & 1 deletion system/base/components/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public function getRouteSettings($requestMethod, System $instance, Auth $auth){
// build out models and return models array
foreach ($settings['models'] as $modelClass) {
if(class_exists($modelClass)){
$models[$modelClass] = new $modelClass();
$models[$modelClass] = new $modelClass();
}else{
$models[$modelClass] = NULL;
}
Expand Down
5 changes: 5 additions & 0 deletions system/base/components/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ public static function on($eventName, callable $eventHandler){
static::$instance->setCustomEvent($eventName, $eventHandler);
}

public static function getRequestingDeviceType(){

;
}

public function getFaultedMiddlewares(){

return $this->faultedMiddlewares;
Expand Down
Loading

0 comments on commit 6cc23d5

Please sign in to comment.