phalcon CURD簡單用法

在自學的時候,感覺國內用的人真的是少,資料也很少,所以簡單記錄下關於CURD的用法!

一、初始化

數據庫中有一張user表,字段爲ID  name

那麼如果要新添加一條記錄該怎麼做呢?

首先在models文件下建一個對應的類繼承於\Phalcon\Mvc\Model

裏面設定變量public $id;     public $name; 對應於數據庫

        public function getSource(){
return "user";
}
public function getId(){
return $this->id;
}
public function getName(){
return $this->name;
}
public function setName($name){
$this->name = $name;
}

這裏getSource裏的返回值爲數據庫中表的名字,如果類名與數據庫表名相同,則不用加這個方法

後面3個方法分別設定id可讀 name可讀 name可寫

二、讀取操作
一般這些對數據庫的操作都是放在模型類裏面的

比如現在要查找id爲1的數據

        public function find(){
$data = $this->find("id = '1'");
return $data;
}

public function find(){
$data = $this->findFirst("id = '1'");
return $data;
}

find返回的是類的集合,findFirst返回的則是一條數據

那麼這麼讀取呢

先寫find的讀取方法

既然返回的是類的集合,那麼就用foreach遍歷

一般這些對數據庫的操作都是放在模型類裏面的
foreach ($data as $val) {
echo $val->name;
}

findFirst就相當於一個$val

直接讀取就可以了

echo $data->name;

find裏的參數:

參數 描述 舉例
conditions Search conditions for the find operation. Is used to extract only those records that fulfill a specified criterion. By default Phalcon\Mvc\Model assumes the first parameter are the conditions. “conditions” => “name LIKE ‘steve%’”
columns Return specific columns instead of the full columns in the model. When using this option an incomplete object is returned “columns” => “id, name”
bind Bind is used together with options, by replacing placeholders and escaping values thus increasing security “bind” => array(“status” => “A”, “type” => “some-time”)
bindTypes When binding parameters, you can use this parameter to define additional casting to the bound parameters increasing even more the security “bindTypes” => array(Column::BIND_TYPE_STR, Column::BIND_TYPE_INT)
order Is used to sort the resultset. Use one or more fields separated by commas. “order” => “name DESC, status”
limit Limit the results of the query to results to certain range “limit” => 10 / “limit” => array(“number” => 10, “offset” => 5)
group Allows to collect data across multiple records and group the results by one or more columns “group” => “name, status”
for_update With this option, Phalcon\Mvc\Model reads the latest available data, setting exclusive locks on each row it reads “for_update” => true
shared_lock With this option, Phalcon\Mvc\Model reads the latest available data, setting shared locks on each row it reads “shared_lock” => true
cache Cache the resultset, reducing the continuous access to the relational system “cache” => array(“lifetime” => 3600, “key” => “my-find-key”)
hydration Sets the hydration strategy to represent each returned record in the result “hydration” => Resultset::HYDRATE_OBJECTS
作爲一個數組的形式放在find裏

三、添加操作

比如現在要添加一個name爲sck的記錄

代碼爲:

public function add(){
$this->name = 'sck';
$this->save();
}

說明一下,在phalcon中,數據庫的具體操作都是封裝好的,而我們現在創建的這個類繼承於\Phalcon\Mvc\Model,這裏本身就已經有很多方法了,可以var

_dump出來看一下的,我們只要對一個空的$this 直接賦值 ,最後通過save這個方法就可以直接對數據庫進行操作了。

四、更新操作

這個和添加操作其實是很類似的

比如現在要對之前name爲sck的記錄進行更新

代碼爲:

public function update(){
$data = $this->find("name = 'sck'");
$data->name = 'scksck';
$data->save();
}

必須要將數據先讀取到$data中,$data是一個新的類,然後直接對裏面的數據進行修改,最後在save一下就可以了

五、刪除操作

比如現在要把id爲1的記錄刪除

代碼爲:

一般這些對數據庫的操作都是放在模型類裏面的
public function delete(){
$data = $this->find("id = '1'");
$data->delete();
}

也是要先將數據讀取到$data

一般這些對數據庫的操作都是放在模型類裏面的
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章