一个月的培训过了一大半了,东西学了很多,但是还是会感觉到患得患失的感觉。这周学了很多也很重要,而且
还参与了实际项目的开发,这对我的成长很大。最难的要数phalcon的学习了,其实也不是phalcon难学,而是我
往往就是把phalcon的框架搭起来就不知道干什么了。就拿写API接口来说吧,我把phalcon的模型写了,却不知道
怎么写sql和语法来运作这个接口这是最烦的。语法基础太差了,还是得多些代码才行。还有就是学英语,英语我是
真的无力吐槽了从初一开始到现在困扰我的东西,不是它我的大学也不会太差。英语记起来好烦啊,就是几个字母组
起来的东西,好难记住。然后又学习了Ajax,dot和sui,这个学起来就好多了,毕竟接触过。天气终于有所下降
了,不再是40度的高温了,心也没有上周那么的烦躁了。学起来也会好很多。
模型是一个继承自 PhalconMvcModel 的一个类。 它必须放到 models 文件夹。一个模型文件必须包含一个
类, 同时它的类名必须符合驼峰命名法:
默认情况下,模型 “Robots” 对应的是数据库表 “robots”, 如果想映射到其他数据库表,可以使用
getSource() 方法:
<?php
class Robots extends PhalconMvcModel
{
public function getSource()
{
return "the_robots";
}
}
控制器类必须以“Controller”为后缀,一个控制器类的例子如下:
<?php
class FansController extends PhalconMvcController
{
/**
* [fans_list 查看api要求的基本粉丝信息,api文档中的多表返回信息]
* @param [对象类型] $app [注入调用的对象]
* @param [时间戳] $mt [用来计算运行时间的变量]
* @return [数组] $responseObj [返回值]
*/
public function fans_list($app, $mt, $responseObj){
$request = new PhalconHttpRequest();
$user_uid = $request->getPost("user_uid");
$phql = "select User.user_name as name, User.user_uid as uid, User.user_portrait as pic, (select sum(money) from Makemoney where uid in (select fans_id from Fans where focus_id = '".$user_uid."')) as money from User where User.user_uid in (select fans_id from Fans where focus_id = '".$user_uid."')";
$rows = $app->modelsManager->executeQuery($phql);
$fans = array();
$i = 0;
foreach ($rows as $row) {
$fans[$i]['name'] = $row->name;
$fans[$i]['pic'] = $row->pic;
$fans[$i]['total_consume'] = $row->money;
$i++;
}
$responseObj['status'] = 1;
$responseObj['msg'] = 'ok';
$responseObj['data'] = $fans;
$responseObj['timeSpend'] = microtime() - $mt.'ms';
return $responseObj;
}
服务可以是控制器,控制器类通常会从服务容器中请求。据于此, 任何一个用其名字注册的类都可以轻易地用一个
控制器来替换:
$di->set('FansController', function(){
$FansController = new FansController();
return $FansController;
});
$app->post('/api/fans_list', function() use ($app, $responseObj) {
$data = $app->FansController->fans_list($app, $startTime, $responseObj);
$app->response->setJsonContent($data);
$app->response->send();
});
在Phalcon中操作数据库提供了基本的ORM映射方式以及更加复杂的PHQL方式。ORM映射方式支持不写SQL语句直接
操作数据库,基本上已经提供了绝大多数使用场景的支持;另一种更为高级的PHQL方式,支持编写Phalcon化的
SQL语句来操作数据库。这里不编写SQL语句,直接使用ORM映射本身提供的一切功能。
public function insertAction()
{
$customer = new Customer();
$customer->username = 'liyi';
$customer->password = '123456';
$ret = $customer->save();
if($ret){
print_r('插入成功');
} else {
print_r('插入失败');
}
}
1.find方法
Phalcon中提供了静态方法find,采用find方法可以返回表中符合条件的所有数据:
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);
}
调用
foreach进行遍历,如代码所示,对遍历的每一个对象调取属性值,然后赋值给关联数组。
2.findFirst方法
Phalcon中的findFirst方法与find方法的使用方式几乎无异,其区别在于findFirst方法的返回结果为单值,不
需要通过foreach遍历获取每个值,在下面的代码中演示了查询数据库表中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);
}
如果需要查询的内容较多,或者是从防止SQL注入的安全角度来考虑,应当使用参数化的查询语句。比如如果需要从
数据库中查找username=(@用户输入的值)并且password=(@用户输入的值)的用户,那么就应当使用参数化
查询。
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');
}
}
更新数据的方法通常是先查询数据,如果能查到数据就对数据进行赋值,然后save即可。现在要更新用户名为
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('用户名不存在或密码错误');
}
}
Phalcon的提供了delete命令用于删除,现要删除用户名为liyi的数据,代码如下:
public function deleteAction()
{
$customer = Customer::findFirstByUsername('liyi');
if($customer){
$res = $customer->delete();
if($res) {
print_r('删除成功');
} else {
print_r('删除失败');
}
} else {
print_r('用户不存在');
}
}
相关文章推荐: