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

一般这些对数据库的操作都是放在模型类里面的
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值