在自学的时候,感觉国内用的人真的是少,资料也很少,所以简单记录下关于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 |
三、添加操作
比如现在要添加一个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