/** * * @return \ORC\DAO\Dao */ public function getDao() { if (!isset($this->_dao)) { $server_name = $this->_table->getServerName(); $this->_dao = DaoFactory::get($server_name); } return $this->_dao; }
public function UserDemo() { // //现在每个方法都互相影响结果 可注释后在看 // // $dbal = DBAL::select("user"); // $dbal->setDataRowClass('\\APP\\Module\\Test\\DataRow\\Test'); // //将数组排序 // $dbal->orderBy('username desc'); // //查询个别字段 // $dbal->setSelect('username,password'); // $list = $dbal->execute(); // $this->set('list',$list); // //将$list的结果中以username作为数组键 password作为value 组成一个数组 // $toArray = $list->toArray("username","password"); // $this->set("toArray",$toArray); // //将$list的结果中usernanme字段的内容组成一个数组 // $getByName = $list->getByName("username"); // $this->set('getByName',$getByName); // //将$list的结果以password分组显示 // $groupBy = $list->groupBy("password"); // $this->set('groupBy',$groupBy); // //将$list的结果升序排序 // $sort = $list->sort("id"); // $this->set('sort',$sort); // //将$list的结果按指定数组排序 // $array = array('zcs',"qwe"); // $sortByfield = $list->sortByField("username",$array); // $this->set('sortByfield',$sortByfield); // //查找数组中的某个值所对定的数据 // $find = $list->find("username","zcs"); // $this->set('find',$find); //DAO PDO $sql = "SELECT * FROM user WHERE id = ?"; $dao = DaoFactory::get(); //得到一个default的dao,也可以使用dbal的getDao方法 // $dao->beginTransaction(); $dao->prepare($sql); $id = 1; $dao->bindParam(1, $id); $pdo = $dao->execute(); $a = $dao->fetchAll($pdo); $this->set("pdo", $a); }
private function getDao($server_name) { if (!isset($this->_daos[$server_name])) { $this->_daos[$server_name] = DaoFactory::create($server_name); } return $this->_daos[$server_name]; }
/** * 获得新的知识点的编号 * @param int $exam_type_id * @param int $subject_id * @return string编号 */ public function createNewCode($exam_type_id, $subject_id) { // $dbal = DBAL::insert('knowledge_point_id'); // $dbal->set('primary_exam_type', $exam_type_id); // $dbal->set('subject_id', $subject_id); // $dbal->set('id', '@COUNT:=(1)'); // $dbal->setRawDuplicate('id = @COUNT:=(id + 1)'); // $dbal->execute(); // $dao = $dbal->getDao(); $subjectLoader = new Subject(); $subjects = $subjectLoader->getAllSubjects(); if (!isset($subjects[$subject_id])) { return false; } $subject_code = $subjects[$subject_id]['code']; $examTypeLoader = new ExamType(); $types = $examTypeLoader->getAllTypes(); if (!isset($types[$exam_type_id])) { return false; } $exam_type_code = $types[$exam_type_id]['code']; $dao = DaoFactory::get(); $dao->query("INSERT INTO knowledge_point_id (primary_exam_type, subject_id, id) VALUES ({$exam_type_id}, {$subject_id}, @COUNT:=(1)) ON DUPLICATE KEY UPDATE id = @COUNT:=(id + 1)"); $dao->query('SELECT @COUNT'); $data = $dao->fetch(Dao::FETCH_NUM); $id = $data[0]; if ($id < 1) { throw new SystemException('生成知识点编号失败', $dao); } return sprintf('%s-%s-ZSD-%d', $exam_type_code, $subject_code, $id); }
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; } }