public function delete() { $db = Application::getDb(); if ($this->get('id')) { return $db->query("DELETE FROM {$this->tableName} WHERE id=?", array($this->get('id'))); } else { return false; } }
public function __construct($id = 0) { $db = Application::getDb(); if (!empty($id)) { $res = $db->fetchRow("select * from {$this->tableName} where id=?", array($id)); } else { $res = ''; } if (!empty($res)) { $this->data = $res; } }
public function ordersAction() { $db = Application::getDb(); $orders = $db->fetchAll('SELECT o.*, od.status, v.lpn, v.model, v.color, v.seats FROM orders_for_driver o LEFT JOIN vehicles v ON v.id=o.vehicle_id LEFT JOIN orders od ON od.sn=o.sn WHERE o.driver_code=? ORDER BY o.when DESC', array($this->code)); foreach ($orders as &$order) { $order['via'] = json_decode($order['via']); $order['via_coordinates'] = json_decode($order['via_coordinates']); } unset($order); //$ofd = new Model_OrderForDriver(); //$orders = $ofd->fetchPagedList(1, 999999, array('driver_code'=>$this->code), '`when` desc')['rows']; $this->sendResult($orders); }
public function orderDetailAction() { $db = Application::getDb(); $order = $db->fetchRow('SELECT o.*, od.vehicle_id, od.driver_code, od.pickup_coordinates, od.dropoff_coordinates, od.via_coordinates, od.when, v.lpn, v.model, v.color, v.seats FROM orders o LEFT JOIN orders_for_driver od ON od.sn=o.sn LEFT JOIN vehicles v ON v.id=od.vehicle_id WHERE o.sn=?', array($this->sn)); $order['via'] = json_decode($order['via']); $order['via_coordinates'] = json_decode($order['via_coordinates']); $driver = new Model_Driver($order['driver_code']); $order['driver_name'] = $driver->get('name'); $order['driver_phone'] = $driver->get('phone'); //$ofd = new Model_OrderForDriver(); //$orders = $ofd->fetchPagedList(1, 999999, array('driver_code'=>$this->code), '`when` desc')['rows']; $this->sendResult($order); }
function set_params(&$params) { parent::set_params($params); $db = Application::getDb(); $user = Application::getEntityInstance('user'); $table = $user->getTableName(); $keyword = trim($this->getValue('search_keyword')); if ($keyword) { $skeyword = addslashes($keyword); $params['where'][] = "({$table}.name LIKE '%{$keyword}%' OR {$table}.email LIKE '%{$keyword}%' OR {$table}.login LIKE '%{$keyword}%')"; } $role_id = $this->getValue('search_role_id'); if ($role_id) { foreach ($role_id as &$r) { $r = (int) $r; } $coupling_table = $user->getRolesCouplingTableName(); $role_id = implode(',', $role_id); $params['from'][] = "\r\n \t\tINNER JOIN {$coupling_table} ON {$coupling_table}.user_id={$table}.id AND {$coupling_table}.role_id IN({$role_id})\r\n \t"; $params['group_by'][] = "{$table}.id"; } }
public function run() { $sql_scripts = coreResourceLibrary::findAll('deploy_db_script', null, null, 'sql'); ksort($sql_scripts); $db = Application::getDb(); $show_errors_old = $db->getShowErrors(); $db->setShowErrors(false); $db->execute("START TRANSACTION"); $deploy_succeed = true; $executed_this_time = array(); foreach ($sql_scripts as $script_name => $script_list) { if (!$deploy_succeed) { continue; } foreach ($script_list as $script) { $script_succeed = true; $path = $script->path; $sql_errors = array(); echo "Executing {$path}... "; if ($this->wasExecutedBefore($path)) { echo "[SKIPPED]\n"; continue; } $path_abs = coreResourceLibrary::getAbsolutePath($path); $script = file_get_contents($path_abs); $script = str_replace("\r\n", "\n", $script); $bom = pack('H*', 'EFBBBF'); $script = preg_replace("/^{$bom}/", '', $script); $queries = explode(";\n", $script); foreach ($queries as $q) { if (!$script_succeed) { continue; } $q = trim($q); if (!$q) { continue; } $query_suceed = (bool) $db->execute($q); if (!$query_suceed) { $sql_errors[] = $db->getLastError(); } $script_succeed = $script_succeed && $query_suceed; } if ($script_succeed) { echo "[OK]\n"; $executed_this_time[] = $path; } else { echo "[FAILED]\n"; echo "Errors:\n" . implode("\n", $sql_errors); $deploy_succeed = false; } } } if ($deploy_succeed) { foreach ($executed_this_time as $ett) { $this->markScriptAsExecuted($ett); } $db->execute("COMMIT"); } else { $db->execute("ROLLBACK"); } $db->setShowErrors($show_errors_old); }
public static function isEmailExists($email) { $db = Application::getDb(); $res = $db->fetchOne("select count(*) from users where email=?", array($email)); return $res != 0; }
/** * Connects to Database */ protected function connect() { $adapters = Application::getDb(); $this->slaveAdapter = $adapters['reader']; $this->masterAdapter = $adapters['writer']; }
public static function isSnExists($sn) { $db = Application::getDb(); $res = $db->fetchOne("select count(*) from {$this->tableName} where sn=?", array($sn)); return $res != 0; }
public function fetchPagedList($page = 1, $itemsPerPage = 0, array $where = array(), $orderBy = '', array $fields = array(), $sqlNoCache = false) { $tableName = Db::quoteIdentifier($this->tableName); if (empty($fields)) { $field = '*'; } else { $cols = array(); foreach ($fields as $name) { $cols[] = Db::quoteIdentifier($name); } $field = implode(',', $cols); } if (empty($where)) { $where = ''; } else { $where = 'WHERE ' . Db::buildWhere($where); } if (!empty($orderBy)) { $orderBy = 'ORDER BY ' . $orderBy; } if ($itemsPerPage > 0) { $page = intval($page); $page < 1 && ($page = 1); $offset = ($page - 1) * $itemsPerPage; $limit = "LIMIT {$offset},{$itemsPerPage}"; } else { $limit = ''; } if ($sqlNoCache) { $sqlNoCache = 'SQL_NO_CACHE'; } else { $sqlNoCache = ''; } $countSql = "SELECT {$sqlNoCache} count(*) FROM {$tableName} {$where}"; $sql = "SELECT {$sqlNoCache} {$field} FROM {$tableName} {$where} {$orderBy} {$limit}"; $db = Application::getDb(); $total = $db->fetchOne($countSql); $rows = $db->fetchAll($sql); return array('total' => $total, 'rows' => $rows); }