public static function getTableColumns($tablename) { $query = 'DESCRIBE ' . $tablename; $result = null; try { $result = Orm::getConnexion()->prepare($query); $result->execute(); $result = $result->fetchAll(\PDO::FETCH_COLUMN); } catch (\Exception $e) { Orm::logError($query, $e); } Orm::logSql($query); return $result; }
<?php /** * Created by PhpStorm. * User: Kévin * Date: 02/12/2015 * Time: 14:23 */ require_once 'vendor/autoload.php'; // enable/disable logs define('LOG_ACTIVE', false); use App\Orm\Orm, App\Entity\User, App\Orm\QueryManager; Orm::init('127.0.0.1', 'orm', 'root', ''); $user = new User(); for ($i = 1; $i < 11; $i++) { $user->setLogin('simple' . $i); $user->setEmail('simple' . $i . '@mail.fr'); $user->setPassword(sha1('simple' . $i)); $user->save(); } $q = new QueryManager(); // var_dump($q->select('user u')->join('LEFT JOIN test t ON t.user_id = u.id')->where('u.id = 3')->execute()); // select // join // where
public function getItemById($table, $where) { $query = 'SELECT id FROM ' . $table . ' WHERE ' . $where; $result = null; try { $result = Orm::getConnexion()->prepare($query); $result->execute(); $result = $result->fetch(\PDO::FETCH_ASSOC)['id']; } catch (\Exception $e) { Orm::logError($query, $e); } Orm::logSql($query); return $result; }
// Do some magic here $fields = Orm::getTableColumns($tableName); $tabs = 1; $code = "<?php\n\n"; $code .= "namespace App\\Entity;\n\n"; $code .= "use App\\Orm\\QueryManager;\n\n"; $code .= "class {$className} extends QueryManager\n{\n"; $code .= do_tabs($tabs) . "/** PROPERTIES */\n"; $code .= do_tabs($tabs) . 'private $tableName = "' . $tableName . '";' . "\n"; foreach ($fields as $field) { if (strtolower($field) == 'id') { continue; } $code .= do_tabs($tabs) . 'private $' . $field . ";\n"; } $code .= do_tabs($tabs) . 'private $isUnique = "' . Orm::getUniqueColumnName($tableName) . '";' . "\n"; $code .= "\n" . do_tabs($tabs) . "/** SETTER */\n"; foreach ($fields as $field) { if (strtolower($field) == 'id') { continue; } $code .= do_tabs($tabs) . 'public function set' . ucfirst($field) . '($' . $field . ")\n"; $code .= do_tabs($tabs) . "{\n"; $code .= do_tabs($tabs + 1) . '$this->' . $field . ' = $' . $field . ";\n"; $code .= do_tabs($tabs) . "}\n\n"; } $code .= do_tabs($tabs) . "/** GETTER */\n"; foreach ($fields as $field) { if (strtolower($field) == 'id') { continue; }
public function remove() { $this->query = 'DROP TABLE ' . $this->tableName; return Orm::getConnexion()->prepare($this->query)->execute(); }