Last active
March 3, 2017 14:51
-
-
Save Yarkhan/dd9fe5a64d5c7c0b98aca1ca9f5371b8 to your computer and use it in GitHub Desktop.
Tentando fazer um ORM com UMA CLASSE. HUEHUHE
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 Yarkhan\Orm; | |
use Exception; | |
use PDO; | |
/** | |
* Nosso humilde e simples ORM. Que não se torne um pesadelo. | |
* Funcionalidades descaradamente copiadas de outros ORM's | |
*/ | |
class Model{ | |
//Instância PDO | |
protected static $_con = null; | |
//colunas na tabela do banco de dados | |
protected static $_props = []; | |
//nome da tabela | |
protected static $_table_name = ''; | |
//nome da coluna da chave primária do bagulho voador | |
protected static $_primary_key = null; | |
// O plano é que has_many tenha a seguinte estrutura | |
// protected static $_has_many = [ | |
// "estados" => [ | |
// "class" => "Vendor\\Package\\Class", | |
// "junction_table" => "usuario_has_empresa", | |
// "junction_this_pk" => "usuario_idUsuario" | |
// "junction_child_pk" => "empresa_idEmpresa" | |
// ] | |
// ]; | |
protected static $_has_many = []; | |
public static function setCon(PDO $con){ | |
self::$_con = $con; | |
} | |
public static function getCon(){ | |
if ( | |
empty(self::$_con) | |
|| empty(static::$_props) | |
|| empty(static::$_table_name) | |
|| empty(static::$_primary_key) | |
) throw new Exception( | |
'Certifique-se de setar corretamente as propriedades do model: | |
$_props[array],$_table_name[string],$_primary_key[string],$_con[PDO object]' | |
); | |
return self::$_con; | |
} | |
/** | |
select * from children | |
join junction_table on chidren.id = junction_table.children_id | |
join parent on parent.id = junction_table.parent_id | |
*/ | |
public function getChildren($children){ | |
$relation = static::$_children[$children]; | |
$childClass = $relation['class']; | |
// "SELECT ".implode(', ',$childClass::$_props) . | |
// " FROM ".$childClass::$_table_name . | |
// " JOIN ".$relation['junction_table'] . | |
// " ON ".$childClass::$_table_name."." | |
$query = static::getCon()->prepare( | |
"SELECT ".implode(', ',$childClass::$_props) . | |
' FROM :child_table' . | |
' join :junction_table on :child_table.pk = :junction_table.child_pk' . | |
' join :parent_table on :parent_table.pk = :junction_table.parent_pk' . | |
' where :parent_table.pk = :parent_pk' | |
); | |
//FROM | |
$query->bindParam(':child_table',$childClass::$_table_name); | |
//1st join | |
$query->bindParam(':junction_table',$relation['junction_table']); | |
$query->bindParam(':child_table.pk',$childClass::$_table_name.".".$childClass::$_primary_key); | |
$query->bindParam(':junction_table.child_pk',$relation['junction_table'].".".$relation["junction_child_pk"]); | |
//2st join | |
$query->bindParam(':parent_table',static::$_table_name); | |
$query->bindParam(':parent_table.pk',static::$_table_name.'.'.static::$_primary_key); | |
$query->bindParam(':junction_table.parent_pk',$relation['junction_table'].".".$relation["junction_this_pk"]); | |
$query->execute(); | |
var_dump($query); | |
} | |
public function getChild(){ | |
} | |
public function getParent(){ | |
} | |
public static function find($id = null){ | |
$query = static::getCon()->prepare( | |
"SELECT ".implode(static::$_props,', ') . | |
" FROM ". static::$_table_name . | |
" WHERE ".static::$_primary_key." = :id" | |
); | |
$query->bindParam(':id',$id); | |
$query->execute(); | |
if($error = $query->errorInfo()[2]) throw new Exception($error); | |
return $result = $query->fetchObject(static::class); | |
} | |
public static function findAll(){ | |
$query = static::getCon()->prepare( | |
"SELECT ".implode(static::$_props,', ') . | |
" FROM ". static::$_table_name | |
); | |
$query->execute(); | |
return $result = $query->fetchAll(\PDO::FETCH_CLASS,static::class); | |
} | |
public function save($id = null){ | |
if(static::$_primary_key){ | |
$values = []; | |
foreach (static::$_props as $key) { | |
$values[] = "$key = :$key"; | |
} | |
$query = static::getCon()->prepare( | |
"UPDATE ".static::$_table_name . | |
" SET ". implode(', ', $values) . | |
" WHERE ".static::$_primary_key." = :id" | |
); | |
foreach (static::$_props as $key) { | |
$query->bindParam(":$key",$this->$key); | |
}; | |
$query->bindParam(":id", $this->{static::$_primary_key},\PDO::PARAM_INT); | |
return $query->execute(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment