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; } }
public function onShutDown() { //save to db $dbal = DBAL::insert($this->table); $dbal->set('session_id', $this->getId()); $dbal->set('value', gzcompress(serialize($this->getAllData()))); $dbal->set('created', Util::getNow()); $dbal->set('updated', Util::getNow()); $dbal->setDuplicate(array('value', 'updated')); $dbal->execute(); }
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; }
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(); }
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(); } } }
/** * 保存数据(根据主键自动选择更新或者插入) * @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; }
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; } }