2010年2月4日 星期四

ZendFramework 代碼片段

config.ini 文件
[general]
db.adapter = PDO_MYSQL
db.config.host = localhost
db.config.username = root
db.config.password =
db.config.dbname = zftest

$config=new Zend_Config_Ini('./config/config.ini','general');
//zend_config_ini加載配置文件用
$db=Zend_Db::factory($config->db->adapter,
$config->db->config->toArray());
Zend_Db::factory("資料庫的類型","把config文件中的内容换成陣列")

防止資料庫攻擊方法:
1、$value=$db->quote('ADFA'DF');
quote自動為單引號行過濾加上雙引号
2、quoteInto()
$value=$db->quoteInto();
多筆查詢


$db->quoteInto('(a =? AND ', $a) .
$db->quoteInfo('b = >)', $b) . 
$db->quoteInfo(' OR ( c != ?)', $c)

3、直接查询
$sql=$db->quoteInto('select * from example where date>?','2006-01-01');
$result=$db->query($sql);
$rows=$result->fetchAll();
4、或者使用占位符号:placeholder 如:
$result=$db->query('select * from exaple where date>:placeholder',
array('placeholder'=>'2006-01-01')
$rows=$result->fetchAll();
5、prepare()方法绑定
$stmt=$db->prepare('select * from example where date>:placeholder');
$stmt->bindvalue('placeholder','2006-01-01');
$stmt->execute();
$rows=$stmt->fetchAll();
6、交易處理
$db->beginTransaction();
try{
$db->query(...);
$db->commit();
}catch(Exception $e){
$db->rollback();
echo $d->getMessage();
}
7、插入數據行
$row=array('title'=>'king','name'=>'baobao',color=>'blue');
$table='uer_table';
$rows_affected=$db->insert($table,$row);
$last_insert_id=$db->lastInsertId();
8、更新數據行
$set=array('name'=>'lailai')
$table='user_table'//更新的数据表
//where語法
$where=$db->quotuinto('name=?','baobao');
//更新資料表,回傳行數
$rows_affected=$db->update($table,$set,$where);
9、刪除資料航
$table='user_table'
where 語句
$where =$db->quoteinto('first_name=?','patsy');
//删除資料並得到影響的行數
$rows_affected=$db->delete($table,$where);
10、取回查詢結果
fetchAll();fetchAssoc();fetchCol();fetchOne();fetchPairs();fetchRow();
//fetchAll取回所有結果集合,並作為連續的雜湊返回
$result=$db->fetchAll(
'select * from round_table where noble_title=:title',
array('title'=>'sir')
);
fetchAssoc()//作为关联数组返回
fetchcol()//取回结果行的第一个字段名
fetchOne()//取回第一个字段值
fetchPairs()//取回一个相关数组,第一个字段为码,第二个字段为值
fetchRow()//取得结果集中的第一行
$config = new Zend_Config_Ini(APP_DIR . '/config/config.ini', 'general');

$this->getHelper('layout')->disableLayout();
$this->getHelper('viewRenderer')->setNoRender();

headScript()->captureStart(); ?>
headScript()->captureEnd(); ?>


$this->view->getHelper('userMemUrl')->userMemUrl('info')

// 分頁頁碼
$this->per = 10;
$len = $this->db->fetchOne("select count(*) from `feedback` where `active`=1 and `memorial_id`=$this->mid");
$pager = new Pager($len, $this->per);
$pager->setAlign('right');
$this->view->pager = $pager->getNavigation();
$start = ($pager->getCurrentPage()-1)*$this->per;
$obj = new Feedback();
$this->view->list = $obj->fetchAll("`active`=1", 'poscode desc', $this->per, $start);


/* 读取上一张,下一张 */
$this->view->prevRow = $obj->fetchRow("`active`=1 and `id`='$row->id' and `poscode`>'$row->poscode'", 'poscode asc', 1, 0);
$this->view->nextRow = $obj->fetchRow("`active`=1 and `id`='$row->id' and `poscode`<'$row->poscode'", 'poscode desc', 1, 0);

上一条:
prevRow): ?>
prevRow->title; ?>


这是第一条


下一条:
nextRow): ?>
nextRow->title; ?>

这是最后一条



$this->view->headLink()->appendStylesheet($this->baseDir . '/css/gardens.css');

$this->view->headScript()->appendFile($this->baseDir . '/js/jquery.js');
$this->view->headScript()->appendFile($this->baseDir . '/js/common.js');


if(!$this->_request->isPost()) {
$this->view->useIframe = true;
} else {
$data = $this->_request->getPost();
if(!$data['content']) {
echo Func::feedtop('alert("生平簡介不能為空,請重新輸入!");');
exit();
}
$row->content = $data['content'];
$rs = $row->save();
if($rs) {
echo Func::feedtop('alert("生平簡介更新成功!");');
exit();
} else {
echo Func::feedtop('alert("生平簡介更新失敗,請稍後重試!");');
exit();
}
}

// ajax 響應
$ajaxContext = $this->_helper->getHelper('AjaxContext');
$ajaxContext->addActionContext('select-wish', 'html')
->addActionContext('send-wish', 'html')
->initContext();


$this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');

$this->_flashMessenger->addMessage('修改成功!');
$this->view->messages = $this->_flashMessenger->getMessages();


echo Func::feedtop('winFunc.go("' .$this->view->getHelper('baseUrl')->baseUrl() . '/user/pay/cart/id/' .$rs. '");');



// 刷新 SESSION
$auth = $this->getInvokeArg('auth');
$auth->getStorage()->write($row);


//驗證碼
$this->vcode = new Zend_Session_Namespace('vcode');
if ($data['vcode'] != $this->vcode->user_login) {
echo Func::feedtop('alert("驗證碼輸入有誤,請確認!");$("#vcode")[0].focus()');
exit();
}

Zend Framework添加CSS 和 JS的方法

$this->view->headLink()->appendStylesheet('/c/css/homepage.css');
$this->view->headLink()->appendStylesheet($baseUrl.'/css/admin/tinybrowser.css');
$this->view->jQuery()->addJavascriptFile($baseUrl . '/js/admin/tiny_functions.js');

Zend Framework轉向

Zend Framework

Zend, PHP
一. render
不指定render
结果: {当前Module}/{当前Controller}/{当前Action}.phtml

$this->render('bar') ;
结果: {当前Module}/{当前Controller}/bar.phtml

二. forward
$this->_forward('bar') ;
结果: {当前Module}/{当前Controller}/bar

$this->_forward('bar', 'foo') ;
结果: {当前Module}/foo/bar

$this->_forward('bar', 'foo', 'hoge') ;
结果: hoge/foo/bar

$params = array(
'a' => '1',
'b' => '2'
) ;
$this->_forward('bar', 'foo', 'hoge', $params) ;
结果: /hoge/foo/bar/a/1/b/2

三. redirect
$this->_redirect('/hoge') ;
结果: /hoge

$this->_redirect('/hoge/foo') ;
结果: /hoge/foo

$this->_redirect('/hoge/foo/bar') ;
结果: /hoge/foo/bar

$this->_redirect('http://localhost/hoge/foo/bar') ;
结果: http://localhost/hoge/foo/bar

$this->_redirect('http://localhost/hoge/foo/bar?a=1&b=2') ;
结果: http://localhost/hoge/foo/bar?a=1&b=2

四. 特殊情况
不使用 layout
结果: $this->_helper->layout()->disableLayout() ;

不使用 view
结果: $this->_helper->viewRenderer->setNoRender() ;

2010年1月1日 星期五

MVC與肯X基

MVC與肯X基

晚上睡不著覺,妹妹在肯X基上班,跟他聊上班的事情,聊著聊著感覺很熟悉
MVC架構為什麼跟肯X基的的工作流程這麼像,所以就記錄下來,希望對想了解
架構的人能有所幫助。

註:mvc是Xeroe PARC在20世紀80年代為程式語言Smalltalk-80所發明的一種開發模式


演出人員:

櫃檯V小姐(通常都要有點好看,所以view很重要)
總配C阿姨(通常就是看客人多少負責發落漢堡手、麵包手做事的人)
漢堡手M1、麵包手M2、飲料手M3(就是壓麵包然後做漢堡出來跟把炸雞的人)
最後當然還有客人(就是所謂的Client、EndUser)


動作是這樣的
1.等了好久客人終於進門了
2.櫃檯V小姐展示一下產品並問客人說請問你要點什麼
3.客人:我要一份X號餐(一份漢堡、一杯可樂、一份薯條)
4.櫃檯V小姐把他key在收銀機上
5.總配C阿姨從後面的螢幕上看到了客人訂餐的資料
6.依照客人的需要總配C阿姨發落漢堡手M1、麵堡手M2及飲料手M3製作各自的東西
7.漢堡手去找漢堡皮、肉片、萵苣絲,薯條手開始鏟薯條,飲料手開始做飲料
8.總配檢查了一下做好的東西,看看是內用還是點餐車道,把它呈上透明的保溫箱裡或外帶的車道窗口
9.櫃檯小姐把產品拿給客人


如果換成網頁程式的MVC架構是這樣解釋的
1.也是在等使用者上這個網站
2.使用者GET網頁,展示網頁畫面至使用者的瀏覽器(VIEW)
3.使用者點擊某一頁,告訴Server我要這一頁
4.無對應的動作
5.Ctrl接受到使用者的要求
6.依照使用者點擊的頁面的功能,開始跟Model要資料
7.Model會依需求跟資料庫來自同一個表或不同的表查詢出資料,並做成Ctrl所能接受的資料
8.Ctrl依照剛剛使用者的要求,看回傳給View用頁面呈現,還是Ajax來呈現
9.View將所需求的資料呈現出來

2009年12月25日 星期五

Zend FrameWork的交易

$this->db->beginTransaction();

try {

//執行SQL語法

$this->db->insert('dbname' , array('欄位1' => '值1'));
$this->db->insert('dbname' , array('欄位2' => '值2'));
$this->db->insert('dbname' , array('欄位3' => '值3'));
$this->db->commit();//成功就執行
}catch (Zend_Db_Exception $e){
$this->db->rollback();//失敗就回去上一步
throw $e;

}

2009年12月13日 星期日

PHP的框架及ORM框架

PHP框架

CoreMVC官方網站
CodeMVC_google

fleaphp

ThinkPHP

CakePHP

CodeIgniter


EasyFramework


戲法人人會變,看你喜歡用哪一種摟

最近在看阿六文章

有段玩笑話說
使用Zend就像加入共產黨
FleaPHP像是改革開放
ThinkPHP開始實施共產主義
所有都試過後發現無黨派的最自由

PHP的ORM框架

doctrine

ZendFramework資料庫的設置

ZendFramework設定資料庫時config.ini,跟ROR一樣有個專門的設定檔

[general]
db.adapter=PDO_MYSQL //使用PDO的MySQL接口
db.config.host=localhost //MySQL的主機IP
db.config.username=root //填入資料庫的使用者
db.config.password= //填入資料庫的密碼
db.config.dbname=zendoophp //填入使用的資料庫名稱



然後在單一進入點的index.php
註冊這組設定
$config=new Zend_Config_Ini('./application/config/config.ini',null, true);
Zend_Registry::set('config',$config);

$dbAdapter=Zend_Db::factory($config->general->db->adapter,
$config->general->db->config->toArray());

設定資料庫接口從Zend_Db::factory(接口,設定陣列);

$dbAdapter->query('SET NAMES UTF8');
先送出一個SQL,將資料庫的連線語系設定為中文