public function startConnection() { if (!empty($this->config->database) && is_null(self::$connection)) { self::$connection = new Connection($this->config->database); if (defined('DEBUG') && DEBUG === TRUE) { self::$connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING); } Model::_setConnection(self::$connection); } else { } }
<?php use core\Classes\classGenerator; use core\ORM\myORM; require_once 'autoload.php'; \core\Connection::setDBHost($argv[1]); \core\Connection::setDBUser($argv[2]); //core\Connection::setDBPass($argv[3]); \core\Connection::setDBName($argv[4]); $Entity = new classGenerator($argv[4], $argv[5], $argv[6]); $ORM = new myORM(core\Connection::getConnection()); $ORM->getValueFromAnnotation('entity\\' . $argv[5]);
public function __construct() { $this->PDO = \core\Connection::getConnection(); }
`password` varchar(32) COLLATE utf8_unicode_ci NOT NULL, `name` varchar(80) COLLATE utf8_unicode_ci NOT NULL, `email` varchar(100) COLLATE utf8_unicode_ci NOT NULL, `address` varchar(150) COLLATE utf8_unicode_ci NOT NULL, `gender` varchar(10) COLLATE utf8_unicode_ci NOT NULL, `admin` tinyint(1) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `users_user` (`user`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci', 'CREATE TABLE IF NOT EXISTS `news`( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `title` varchar(500) COLLATE utf8_unicode_ci NOT NULL, `body` text COLLATE utf8_unicode_ci NOT NULL, `image` tinyint(1) NOT NULL, `section` varchar(20) NOT NULL, `created` timestamp DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci', 'CREATE TABLE IF NOT EXISTS `comments`( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `newsId` int(10) unsigned NOT NULL, `userId` int(10) unsigned NOT NULL, `comment` text COLLATE utf8_unicode_ci NOT NULL, `created` timestamp DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci'); foreach ($queries as $value) { $statement = Connection::prepare($value); $statement->execute(); } User::insert(array('user' => 'admin', 'password' => md5('admin'), 'name' => 'admin', 'email' => '*****@*****.**', 'address' => 'admin', 'gender' => 'man', 'admin' => 1)); Connection::disconnect();
<?php require "./vendor/autoload.php"; $controllerName = '\\App\\C\\' . Core\Params::post('c', Core\Params::get('c', 'Reservation')) . 'Controller'; $actionName = Core\Params::post('a', Core\Params::get('a', 'liste')) . 'Action'; //connect to database \Core\Connection::setup('mysql:host=localhost;dbname=name', 'user', 'password', array(\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'')); chdir('../'); \Core\Controller::redirect($controllerName, $actionName, array_merge(Core\Params::post(), Core\Params::get())); \Core\Controller::getView()->display();
/** * Save method, save current Model (Insert or Update) * @return bool */ public function save() { if (!$this->preSave()) { return FALSE; } $reference = $this->_getReference(); $pk = $this->_getPK(); $data = get_object_vars($this); if (empty($pk) && $pk !== NULL) { throw new \Exception('Model PK is empty!'); } if (is_array($pk)) { $pkv = []; $_pkv = []; $insert = TRUE; foreach ($pk as $c) { if (!empty($data[$c])) { $pkv[] = $data[$c]; $_pkv[$c] = $data[$c]; // unset($data[$c]); $insert = FALSE; } } if (!$insert) { $insert = is_null(self::getWhere($_pkv)); } } else { $pkv = !empty($data[$pk]) ? $data[$pk] : NULL; $_pkv = [$pk => $pkv]; // Se for AUTO INCREMENT remove dos campos a serem INSERIDOS/ALTERADOS if (self::_isPkAi()) { unset($data[$pk]); } $insert = empty($pkv); } unset($data['__error']); foreach ($data as $key => $value) { if (strpos($key, '_') === 0) { unset($data[$key]); } } if ($insert) { if (array_key_exists('created', $data)) { $data['created'] = Date('Y-m-d H:i:s'); } if (array_key_exists('modified', $data)) { unset($data['modified']); } $res = self::$connection->insert($reference, $data)->execute(); if ($res && $pk !== NULL && !is_array($pk)) { $this->{'set' . $pk}(self::$connection->lastInsertId()); } $this->afterSave($res); return $res; } else { if (array_key_exists('modified', $data)) { $data['modified'] = Date('Y-m-d H:i:s'); } if (array_key_exists('created', $data)) { unset($data['created']); } $res = self::$connection->update($reference, $data, $_pkv)->execute(); $this->afterSave($res); return $res; } }