Created
November 3, 2017 16:39
-
-
Save kobus1998/490f288d44fcc712dd41fb2b6607ff7c to your computer and use it in GitHub Desktop.
PHP Database builder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// requires and namespaces | |
$userTable = new Migrations\Table('users', function ($field) { | |
$field->primaryKey('id')->unsigned(); | |
$field->varChar('username'); | |
$field->integer('age')->nullable(); | |
}); | |
$postTable = new Migrations\Table('posts', function ($field) { | |
$field->primaryKey('id')->unsigned(); | |
$field->varChar('title'); | |
$field->varChar('body')->nullable(); | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace my\name\space; | |
class Field { | |
private $table; // parent table is Table class | |
public $type; // field type, i.e. varchar, string etc.. | |
public $name; // field name | |
public $isNullable = false; | |
public $isUnSigned = false; | |
public $hasDefault = false; | |
// set parent table and push this class instance | |
function __construct ($table) { | |
$this->table = $table; | |
array_push($this->table->fields, $this); | |
} | |
// create new class and set some field | |
public function newField ($name, $type) { | |
$field = new Field($this->table); | |
$field->name = $name; | |
$field->type = $type; | |
return $field; | |
} | |
public function primaryKey ($name) { | |
return $this->newField($name, 'AUTO_INCREMENT'); | |
} | |
public function varChar ($name) { | |
return $this->newField($name, 'var_char'); | |
} | |
public function integer ($name) { | |
return $this->newField($name, 'integer'); | |
} | |
public function boolean ($name) { | |
return $this->newField($name, 'boolean'); | |
} | |
public function date ($name) { | |
return $this->newField($name, 'date'); | |
} | |
public function nullable () { | |
$this->isNullable = true; | |
return $this; | |
} | |
public function unsigned () { | |
$this->isUnSigned = true; | |
return $this; | |
} | |
public function default ($default) { | |
$this->hasDefault = $default; | |
return $this; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace my\name\space; | |
class Table { | |
public $type = 'innoDB'; | |
public $fields = []; | |
public $name; | |
function __construct($name, $fields) { | |
$this->name = $name; | |
$fields(new Field($this)); | |
array_splice($this->fields, 0, 1); | |
} | |
public function setType ($type) { | |
$this->type = $type; | |
return $this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment