public function execute() { $rs = $this->getRequest(); switch ($rs->get('action')) { case "insert": $dbal = DBAL::insert('user'); $dbal->set('username', $rs->get('username')); $dbal->set('password', $rs->get('password')); $id = $dbal->execute(); return $this->HTMLRedirect($this->generateURL('Second.Demo'), '注册成功', '注册成功'); break; case "list": $page = $rs->get("page"); $model = $this->getModel('Second.User'); $model->loadUsers($page); return $this->HTMLView('Second.Add'); break; case "details": $rs = $this->getRequest(); $uid = $rs->get('uid'); $model = $this->getModel('Second.Details'); $model->loadDetails($uid); return $this->HTMLView('Second.Details'); break; default: return $this->HTMLView('Second.List'); break; } }
/** * * @param int $course_id * @return \APP\Module\Course\DataRow\Course */ public function get($course_id) { $dbal = DBAL::select('courses'); $dbal->setDataRowClass('\\APP\\Module\\Course\\DataRow\\Course'); $dbal->byId($course_id); return $dbal->getOne(); }
public function loadDetails($uid) { $dbal = DBAL::select('default.user'); $dbal->setDataRowClass('\\APP\\Module\\Test\\DataRow\\Test'); $dbal->byId($uid); $rs = $dbal->getOne(); $this->set('Details', $rs); }
public function loadUsers($page) { $dbal = DBAL::select('user'); $dbal->setPage($page, '2'); $dbal->setDataRowClass('\\APP\\Module\\Test\\DataRow\\Test'); $list = $dbal->execute(); $pagination = new Pagination($list, $dbal->getTotalCount(), $page, 2); $this->set('list', $list); $this->set('patgination', $pagination); }
public function del($id) { $pk = $this->_table->getPrimaryKey(); $dbal = DBAL::update($this->_table); $dbal->pk($id); $result = $dbal->execute(); $this->flushAllIdsCache(); //may suffer order by field $this->flushItemCache($id); return $result; }
public static function getAllPermissions() { static $permissions; if (!isset($permissions)) { $permissions = array(); $result = DBAL::select(DB::getPermissionTableName())->execute(); foreach ($result as $dataRow) { $permissions[$dataRow->get('role_id')] = explode(',', $dataRow['permissions']); } } return $permissions; }
public function save(array $item) { $pk = $this->_table->getPrimaryKey(); if (isset($item[$pk])) { throw new SystemException('Duplicate Primary Key set for CRUD::Create'); } $dbal = DBAL::insert($this->_table); foreach ($item as $key => $value) { $dbal->set($key, $value); } $id = $dbal->execute(); $this->flushAllIdsCache(); return $id; }
/** * @return \ORC\App\User\Roles */ public static function getAllRoles() { static $roles; if (!isset($roles)) { $roles = new self(); $dbal = DBAL::select(DB::getRoleTableName()); $result = $dbal->execute(); foreach ($result as $row) { $role = new Role($row); //$this->_roles[$role->getId()] = $role; $roles[$role->getId()] = $role; } } return $roles; }
public function __invoke($cacher, $key, &$value) { $expiration = $this->getCacheExpiration(); $pk = $this->_table->getPrimaryKey(); $dbal = DBAL::select($this->_table); $dbal->setSelect($pk); if ($this->_order_by) { $dbal->orderBy($this->_order_by); } $value = $dbal->execute()->getByName($pk); if ($expiration) { $cacher->set($key, $value, $expiration); return false; } return true; }
protected function start() { if ($this->started()) { return true; } $dbal = DBAL::select($this->table); $dbal->bySessionId($this->getId()); $row = $dbal->getOne(); if ($row) { $data = @unserialize(gzuncompress($row['value'])); if (is_array($data)) { $this->_data = $data; } } $this->session_started = true; }
protected function saveToDb(array $result, $index = 0) { $table = $this->getTable(); $fileInfo = $result['info']; $dbal = DBAL::insert($table); $dbal->set(array('name' => $fileInfo->getName(), 'filepath' => $fileInfo->getPath(), 'mime' => $fileInfo->getType(), 'filesize' => $fileInfo->getSize(), 'created' => Util::getNow(), 'options' => serialize($this->getOptions()))); if (!empty($this->_extra)) { foreach ($this->_extra as $k => $v) { //@todo consider to add some callback feature $dbal->set($k, $v); } } if (method_exists($this, 'preSaveToDb')) { $dbal = $this->preSaveToDb($dbal, $result, $index); } return $dbal->execute(); }
public function __invoke(ICacher $cacher, array $keys, array &$values) { $expiration = $this->getCacheExpiration(); $dbal = DBAL::select($this->_table); $dbal->pk($keys); if ($this->_row_classname) { $dbal->setDataRowClass($this->_row_classname); } $values = $dbal->execute()->toArray($this->_table->getPrimaryKey()); foreach ($keys as $key) { if (!isset($values[$key])) { $values[$key] = ICacher::EMPTY_VALUE; } } if ($expiration) { $cacher->setMult($values, $expiration); return false; } return true; }
public function save(array $item) { $pk = $this->_table->getPrimaryKey(); if (!isset($item[$pk])) { throw new SystemException('Primary Key needed for CRUD::Update'); } $dbal = DBAL::update($this->_table); $dbal->pk($item[$pk]); foreach ($item as $key => $value) { if ($key == $pk) { continue; } $dbal->set($key, $value); } $result = $dbal->execute(); $this->flushAllIdsCache(); //may suffer order by field $this->flushItemCache($item[$pk]); return $result; }
/** * * @param string $table_name * @param bool $cache * @return \ORC\DAO\Table\DataList */ public static function getTableData($table_name, $cache = true) { if ($cache) { $cacheKey = sprintf('table_%s', $table_name); $cacher = CacheFactory::get(null, 'db'); $data = $cacher->get($cacheKey); if (is_object($data)) { return $data; } if (@strcmp($data, ICacher::EMPTY_VALUE) == 0) { return new DataList(array(), new Table($table_name)); } } $dbal = DBAL::select($table_name); $list = $dbal->execute(); if ($cache) { if ($list) { $cacher->set($cacheKey, $list); } else { $cacher->set($cacheKey, ICacher::EMPTY_VALUE); } } return $list; }
public function execute() { $request = $this->getRequest(); //注意!为了演示dao和dbal的用法,把所有代码放在这里,但实际上要利用model switch ($request->get('action')) { case 'add': return $this->HTMLView('DB.Add'); break; case 'save': $key1 = $request->get('key1'); $key2 = $request->get('key2'); $dbal = DBAL::insert('test'); $dbal->set('key1', $key1); $dbal->set('key_2', $key2); //$dbal->setKey2($key2);//两种写法都可以,推荐用上面的 $id = $dbal->execute(); return $this->HTMLRedirect($this->generateURL('DB.Demo'), '保存成功', '页面保存成功'); break; case 'view2': //利用key2作为主键 $key2 = $request->get(0, 'safe', ''); //注意request的get的用法 $dao = DaoFactory::get('default'); //得到一个default的dao,也可以使用dbal的getDao方法 $dao->beginTransaction(); //开始一个事务, 注意这里select没有加锁(select for update) $dbal = DBAL::select('default.test'); //default是配置文件里的server name,不是database name $dbal->setDataRowClass('\\APP\\Module\\Test\\DataRow\\Test'); //指定datarow class,否则使用ORC\DAO\Table\DataRow $dbal->byKey2($key2); //注意虽然在数据库中字段是key_2,这里不要有_ $row = $dbal->getOne(); //给计数器加1 $dbal = DBAL::update('test'); $dbal->increase('count', 1); $dbal->execute(); $dao->commit(); //提交 $model = $this->getModel('DB.Test'); $model->set('record', $row); return $this->HTMLView('DB.ViewDemo'); break; case 'view': $id = $request->get('id', 'posint', 0); $dbal = DBAL::select('default.test'); //default是配置文件里的server name,不是database name $dbal->setDataRowClass('\\APP\\Module\\Test\\DataRow\\Test'); //指定datarow class,否则使用ORC\DAO\Table\DataRow $dbal->byId($id); $result = $dbal->getOne(); //直接得到一个DataRow,这里直接得到Test对象 $model = $this->getModel('DB.Test'); $model->set('record', $result); return $this->HTMLView('DB.ViewDemo'); break; case 'list': default: $dbal = DBAL::select('test'); //获得一个dbal select对象 $dbal->setDataRowClass('\\APP\\Module\\Test\\DataRow\\Test'); //指定datarow class,否则使用ORC\DAO\Table\DataRow $list = $dbal->execute(); $model = $this->getModel('DB.Test'); $model->set('list', $list); //如果要分页的话 $dbal = DBAL::select('test'); //获得一个dbal select对象 $dbal->setDataRowClass('\\APP\\Module\\Test\\DataRow\\Test'); //指定datarow class,否则使用ORC\DAO\Table\DataRow $dbal->setPage(1, 20); //第一页,每页20条 $list = $dbal->execute(); $pagination = new Pagination($list, $dbal->getTotalCount(), 1, 20); return $this->HTMLView('DB.List'); break; } }
private function getRoles() { //read the role from table every time to make sure it's correct static $roles; if (isset($roles)) { return $roles; } $dbal = DBAL::select('admin_user_roles'); $dbal->byUserId($this->getId()); $roles = $dbal->execute()->getByName('role_id'); return $roles; }
public function getAllKnowledgePoints() { $catalogs = $this->getCatalogs(); $catalog_ids = $catalogs->getByName('id'); $dbal = DBAL::select('course_catalog_kps'); $dbal->byCourseCatalogId($catalog_ids); $list = $dbal->execute(); $list->groupBy('course_catalog_id'); }
/** * 保存数据(根据主键自动选择更新或者插入) * @param array $data * @throws SystemException * @return Ambigous <\ORC\DBAL\Common\int/string, boolean> */ public function save(array $data) { $table = new Table($this->table_name); $pk = $table->getPrimaryKey(); if ($pk) { $pks = explode(',', $pk); } else { $pks = array(); } $is_new = true; $pk_values = array(); $values = array(); foreach ($data as $key => $value) { if (in_array($key, $pks)) { //说明有主键值,是更新 $is_new = false; $pk_values[$key] = $value; } else { $values[$key] = $value; } } if ($is_new == false) { if (count($pks) != count($pk_values)) { throw new SystemException('主键数量不匹配'); } //如果是指定pk值但是是新建 $is_update = false; $data = $this->getAll(); //比较pk vlaue foreach ($data as $row) { $found = true; foreach ($pk_values as $k => $v) { if ($row->get($k) != $v) { $found = false; break; } } if ($found) { $is_update = true; break; } } if ($is_update == false) { $is_new = true; //说明虽然指定了主键值,但是数据库里找不到对应的值,所以仍然是插入 } } if ($is_new) { $dbal = DBAL::insert($this->table_name); } else { $dbal = DBAL::update($this->table_name); foreach ($pk_values as $key => $value) { $dbal->findBy($key, array($value)); } } foreach ($values as $key => $value) { $dbal->set($key, $value); } $result = $dbal->execute(); $this->flushCache(); return $result; }
protected function scanActions($module, $path, $extra_folder = '') { $iterator = new \DirectoryIterator($path); foreach ($iterator as $fileinfo) { if ($fileinfo->isDot()) { continue; } if ($fileinfo->isDir()) { $this->scanActions($module, $fileinfo->getPathname(), $extra_folder . DIRECTORY_SEPARATOR . $fileinfo->getFilename()); continue; } $filename = $fileinfo->getFilename(); if (preg_match('/^([a-z0-9_]+)\\.action\\.php$/i', $filename, $matches)) { $action_name = ltrim(str_replace(DIRECTORY_SEPARATOR, '_', $extra_folder) . '_' . $matches[1], '_'); $class_name = sprintf('%s_%s_Action', $module, $action_name); //user a parser or not //var_dump($class_name, class_exists($class_name)); //@TODO not implement, will user route annotation later $action_info = array(); $action_info['action_name'] = str_replace('_', '.', $action_name); $action_info['module'] = $module; $action_info['url'] = sprintf('/%s/%s', $module, str_replace('_', '/', $action_name)); $action_info['filename'] = substr($fileinfo->getPathname(), strlen(DIR_APP_MODULE_ROOT) + 1); //var_dump($action_info); // require_once $fileinfo->getPathname(); // $ref = new ReflectionClass($class_name); // $doc = $ref->getDocComment(); // $doc = explode(PHP_EOL, $doc); // foreach ($doc as $line) { // $line = trim($line); // if (preg_match('/[\s*]+@Route\s*(.*)$/i', $line, $matches)) { // } // } $dbal = DBAL::insert(\ORC\Util\Route::TABLENAME_ROUTES); $dbal->set($action_info); $dbal->setDuplicate(array('action_name', 'module', 'filename')); $dbal->execute(); } } }
public function autoLogin($id) { $this->flushRoles(); $dbal = DBAL::select(DB::getUserTableName())->pk($id); $result = $dbal->getOne(); if ($result instanceof \ORC\DAO\Table\DataRow) { $this->_data = $result->getAllData(); return true; } return false; }