摘要:本文描述了框架中數(shù)據(jù)庫操作方法,主要討論框架的組件中的操作方法。屬性方法在框架中支持屬性的擴(kuò)展查詢,在上例中,可以把條件語句改為同時(shí)省略查詢條件結(jié)果不變。
本文描述了PHP-Phalcon框架中數(shù)據(jù)庫操作方法,主要討論P(yáng)halcon框架的Model組件中的操作方法。更詳細(xì)的Model介紹請參考:官方文檔
在Phalcon框架中,通過在DI中注入db參數(shù)來實(shí)現(xiàn)數(shù)據(jù)庫的連接和配置,基本的配置方法如下:
use PhalconDbAdapterPdoMysql as DbAdapter; $di->set("db", function () { return new DbAdapter(array( "host" => "localhost", "username" => "root", "password" => "", "dbname" => "test" )); });
通過在$di中設(shè)置"db"的連接屬性,包括host,username,password,dbname等屬性來獲取數(shù)據(jù)庫參數(shù),配置好db之后就可以利用Phalcon中的ORM框架了。
另一種通過配置文件的方法如下:
1.首先需要將數(shù)據(jù)信息寫入配置文件,在Phalcon中支持ini, php, json等三種配置文件形式,以下為ini形式的配置文件。
[database] adapter = Mysql host = localhost username = root password = dbname = test
2.利用配置文件將數(shù)據(jù)庫信息寫入DI
$di->set("db", function () use ($config) { $config = $config->get("database")->toArray(); $dbClass = "PhalconDbAdapterPdo" . $config["adapter"]; unset($config["adapter"]); return new $dbClass($config); });2. 建立數(shù)據(jù)庫表
在MySQL中建立數(shù)據(jù)庫表,如下所示:
CREATE TABLE `customer` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(255) DEFAULT NULL, `password` varchar(32) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
以上語句建立了一個(gè)customer表,包括主鍵id,以及username和password兩個(gè)字段。
3. 創(chuàng)建模型根據(jù)上述數(shù)據(jù)庫表,創(chuàng)建Customer.php模型類,需要注意的是模型類必須繼承PhalconMVCModel類,并且采用與數(shù)據(jù)庫表名一致的駝峰命名法。因此建立如下的Customer類:
class Customer extends PhalconMvcModel { //采用默認(rèn)的對(duì)應(yīng)規(guī)則會(huì)自動(dòng)映射數(shù)據(jù)庫表的字段,可以不寫 /** * * @var integer */ public $id; /** * * @var string */ public $username; /** * * @var string */ public $password; /** * Returns table name mapped in the model. * * @return string */ public function getSource() { return "customer"; } }
其實(shí)只要滿足了對(duì)應(yīng)的命名規(guī)則,模型類中的屬性是不需要定義的,這里定義的目的是方便開發(fā)過程中查看,并且可以提高性能;此外,如果沒有采用數(shù)據(jù)庫表名對(duì)應(yīng)的類名進(jìn)行命名的話,就需要在初始化函數(shù)中通過setSource方法進(jìn)行配置,配置代碼:
public function initialize() { $this->setSource("tablename"); }
從這里可以看出,在現(xiàn)代的Web開發(fā)框架中,都遵循約定大于配置(CoC)的基本原則。只要遵循約定,就可以很方便的進(jìn)行Web開發(fā)。
4. 數(shù)據(jù)庫操作在Phalcon中操作數(shù)據(jù)庫提供了基本的ORM映射方式以及更加復(fù)雜的PHQL方式。ORM映射方式支持不寫SQL語句直接操作數(shù)據(jù)庫,基本上已經(jīng)提供了絕大多數(shù)使用場景的支持;另一種更為高級(jí)的PHQL方式,支持編寫Phalcon化的SQL語句來操作數(shù)據(jù)庫。這里不編寫SQL語句,直接使用ORM映射本身提供的一切功能。
4.1 添加數(shù)據(jù)如果我想添加一條數(shù)據(jù),最基本的方式演示如下:
新建一個(gè)控制器DatabaseController
在控制器中添加InsertAction
在InsertAction中寫入下列代碼:
public function insertAction() { $customer = new Customer(); $customer->username = "liyi"; $customer->password = "123456"; $ret = $customer->save(); if($ret){ print_r("插入成功"); } else { print_r("插入失敗"); } }4.2 查詢數(shù)據(jù)
1.find方法
Phalcon中提供了靜態(tài)方法find,采用find方法可以返回表中符合條件的所有數(shù)據(jù):
public function findAction() { $customers = Customer::find(); $result = []; foreach ($customers as $customer) { $result[] = [ "username" => $customer->username, "password" => $customer->password, ]; } $this->response->setContentType("application/json", "UTF-8"); return $this->response->setJsonContent($result); }
調(diào)用
2.findFirst方法
Phalcon中的findFirst方法與find方法的使用方式幾乎無異,其區(qū)別在于findFirst方法的返回結(jié)果為單值,不需要通過foreach遍歷獲取每個(gè)值,在下面的代碼中演示了查詢數(shù)據(jù)庫表中username字段值等于"liyi"的記錄。
public function findfirstAction() { $customer = Customer::findFirst("username = "liyi""); $result = [ "username" => $customer->username, "password" => $customer->password, ]; $this->response->setContentType("application/json", "UTF-8"); return $this->response->setJsonContent($result); }
3.findBy<屬性>方法
在Phalcon框架中支持findBy<屬性>的擴(kuò)展查詢,在上例中,可以把條件語句改為findFirstByUsername,同時(shí)省略查詢條件,結(jié)果不變。
$customer = Customer::findFirstByUsername("kirineko");
4.參數(shù)化查詢語句
如果需要查詢的內(nèi)容較多,或者是從防止SQL注入的安全角度來考慮,應(yīng)當(dāng)使用參數(shù)化的查詢語句。比如如果需要從數(shù)據(jù)庫中查找username=(@用戶輸入的值)并且password=(@用戶輸入的值)的用戶,那么就應(yīng)當(dāng)使用參數(shù)化查詢。
下面模擬用戶登錄的過程,用戶輸入用戶名和密碼,然后系統(tǒng)在數(shù)據(jù)庫中進(jìn)行查找,如果找到則返回歡迎頁,如果沒有找到,就提示錯(cuò)誤信息。
public function loginAction() { $username = $this->request->getPost("username"); $password = $this->request->getPost("password"); $conditons = "username = :username: and password = :password:"; $parameters = [ "username" => $username, "password" => $password, ]; $ret = Customer::findFirst( [ $conditons, "bind" => $parameters, ]); if($ret){ print_r("login success"); } else { print_r("login failed"); } }4.3 更新數(shù)據(jù)
更新數(shù)據(jù)的方法通常是先查詢數(shù)據(jù),如果能查到數(shù)據(jù)就對(duì)數(shù)據(jù)進(jìn)行賦值,然后save即可?,F(xiàn)在要更新用戶名為kirineko的密碼為用戶指定的密碼,代碼如下:
public function updateAction() { $password = $this->request->getPost("password"); $newpassword = $this->request->getPost("newpassword"); $conditons = "username = :username: and password = :password:"; $parameters = [ "username" => "kirineko", "password" => $password, ]; $customer = Customer::findFirst([ $conditons, "bind" => $parameters, ]); if($customer){ $customer->password = $newpassword; $customer->save(); print_r("更新成功"); } else { print_r("用戶名不存在或密碼錯(cuò)誤"); } }4.4 刪除數(shù)據(jù)
Phalcon的提供了delete命令用于刪除,現(xiàn)要?jiǎng)h除用戶名為liyi的數(shù)據(jù),代碼如下:
public function deleteAction() { $customer = Customer::findFirstByUsername("liyi"); if($customer){ $res = $customer->delete(); if($res) { print_r("刪除成功"); } else { print_r("刪除失敗"); } } else { print_r("用戶不存在"); } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/30321.html
摘要:傳統(tǒng)的代碼中,在類中調(diào)用其他對(duì)象,都是自己出來一個(gè)對(duì)象,然后調(diào)用,這樣代碼的耦合度就比較高。日志對(duì)象和主程序的耦合度降到最低,即使更改日志對(duì)象的操作,主程序不受影響。 SSM框架網(wǎng)站后臺(tái)搭建(一) 1.所用技術(shù)簡單介紹 1.SSM中的S:Spring Spring在百度詞條上的解釋是: Spring是一個(gè)開放源代碼的設(shè)計(jì)層面框架,他解決的是業(yè)務(wù)邏輯層和其他各層的松耦合問題,因此它將面向...
摘要:中的事件的一個(gè),我暫且理解為一個(gè)中的和這兩個(gè)屬性已經(jīng)在框架中直接掛載在了對(duì)象上,歸功于曾老師。 CQRS是啥?DDD又是啥? 這兩個(gè)概念其實(shí)沒什么神秘的,當(dāng)然此文章中的這兩個(gè)概念以曾老師的課程為準(zhǔn)(關(guān)于CQRS和DDD的標(biāo)準(zhǔn)概念,google上已經(jīng)很多了,不再贅述。) DDD(Domain Driven Design),領(lǐng)域驅(qū)動(dòng)設(shè)計(jì)開發(fā)。 DDD和OOP有什么同嗎?其實(shí)就我個(gè)人經(jīng)驗(yàn)來說...
摘要:框架搭建首先下載相應(yīng)的包,對(duì)于包有兩種方式使用創(chuàng)建依賴從而導(dǎo)入所需的包??偨Y(jié)主要進(jìn)行頁面的請求接受與響應(yīng)。組件包括前端控制器,處理器映射器,處理器適配器,視圖解析器,處理器,視圖。 我之前的文章介紹了如何搭建SSH框架以及如何利用這一框架來進(jìn)行web應(yīng)用開發(fā),最近我又接觸了SSM框架即Spring+SpringMVC+Mybatis三大框架的整合,而且目前該框架就SSH框架而言使用的較...
摘要:近日它們交鋒的戰(zhàn)場就是動(dòng)態(tài)計(jì)算圖,誰能在這場戰(zhàn)爭中取得優(yōu)勢,誰就把握住了未來用戶的流向。所以動(dòng)態(tài)框架對(duì)虛擬計(jì)算圖的構(gòu)建速度有較高的要求。動(dòng)態(tài)計(jì)算圖問題之一的多結(jié)構(gòu)輸入問題的高效計(jì) 隨著深度學(xué)習(xí)的發(fā)展,深度學(xué)習(xí)框架之間競爭也日益激烈,新老框架紛紛各顯神通,想要在廣大DeepLearner的服務(wù)器上占據(jù)一席之地。近日它們交鋒的戰(zhàn)場就是動(dòng)態(tài)計(jì)算圖,誰能在這場戰(zhàn)爭中取得優(yōu)勢,誰就把握住了未來用戶的流...
閱讀 2590·2021-09-26 10:13
閱讀 6010·2021-09-08 10:46
閱讀 698·2019-08-30 15:53
閱讀 2972·2019-08-29 16:13
閱讀 2766·2019-08-26 12:23
閱讀 3495·2019-08-26 11:24
閱讀 1102·2019-08-23 18:09
閱讀 1038·2019-08-23 17:08