这篇内容将对下列操作进行示范:

Insert、Select、Update、Calculation、Transaction、models advanced、dev-tools、cookies

[ Insert ]

(1)

// 模型内操作,data是['字段'=>'值']的一维数组。

$bool = $this->save($data);

return $bool;

(2)

// static

$db = \Phalcon\Di::getDefault()->getShared('db');
// $db = $this->di->getShared('db'); $bool = $db->insert(
"dianzan",
array_values($data), // 顺序对应字段的值数组,不能含空字符串
array_keys($data) // 字段数组
); return $db->lastInsertId();

(3)

// 批量insert

$data[] = [
'site' => $v,
'role_id' => $role_id,
'question_id' => $question_id,
]; $sql = \Alcon\Supports\Helper::build_insert_sql('pic_log', 'site, role_id, question_id', $data); $db = \Phalcon\Di::getDefault()->getShared('db'); $bool = (bool)$db->execute($sql); return $bool;

[ Select ]

(1)

// 模型内操作, 参数都为可选.

$arr = [
// "conditions" => "question_id = 1" // 与下面直接写条件的形式任选一种
"status = 1 AND role_id = 3",
"columns" => "id,isdel",
"order" => "sort DESC, addtime DESC",
"limit" => "2",
]; $all_res = static::find($arr)->toArray(); // 多条 $first_res = static::findFirst($arr)->toArray(); // 单条

(2)

// phalcon query language

$manager = \Phalcon\Di::getDefault()->getShared('modelsManager');

$phql = "SELECT MAX(pic_status) AS pic_status FROM " . __CLASS__ . " WHERE question_id = :question_id:";

$res = $manager->executeQuery($phql, ['question_id' => $question_id])
->getFirst()
->toArray(); return $res['pic_status'];

(3)

// Query Builder

$builder = \Phalcon\Di::getDefault()->getShared('modelsManager')->createBuilder();

$res = $builder->columns(["MAX(" . __CLASS__ . ".pic_status)"])
->from(__CLASS__)
->where("question_id = :question_id:", ["question_id" => $question_id])
->getQuery()->execute()
->getFirst()
->toArray(); return reset($res);

[ Update ]

(1)

$db = \Phalcon\Di::getDefault()->getShared('db');
// $db = $this->di->getShared('db'); $bool = $db->update(
'wenda_log',
['accept_time'], // 字段数组
[time()], // 值数组,对应顺序
"id = '{$answer_id}'" // 条件
); return $bool;

(2)

$db = \Phalcon\Di::getDefault()->getShared('db');

$sql = "UPDATE question_log SET answer_num = answer_num + 1 WHERE question_id =  '{$question_id}'";

return (bool)$db->execute($sql);

(3)

$manager = \Phalcon\Di::getDefault()->getShared('modelsManager');

$phql = "UPDATE " . __CLASS__ . " SET iscollect = :iscollect: WHERE id = '{$id}'";

// $data = ['iscollect' => 1];
$res = $manager->executeQuery($phql, $data); $bool = $res->success(); return $bool;

(4)

// 模型内

$this->save($data);

[ Calculation ]

https://docs.phalconphp.com/en/latest/reference/models.html

// 总和
return static::sum([
'conditions' => 'xxxx',
'column' => 'shownum',
]); // 平均值
return static::average([
'column' => 'shownum',
]); // 最大
return static::maximum([
'column' => 'shownum',
]); // 最小
return static::minimum([
'column' => 'shownum',
]); // 数量 // conditions
return static::count("area='北京'"); // distinct
return static::count([
'distinct' => 'area',
]);

[ Transaction ]

https://docs.phalconphp.com/en/latest/reference/model-transactions.html

$db = $di->get('db');
$db->begin();
$bool = $db->execute("INSERT ...");
if (! $bool) {
$db->rollback();
} else {
$bool = $db->execute("UPDATE ...");
if (! $bool) {
$db->rollback();
} else {
$db->commit();
}
}

[ model advanced ]

https://docs.phalconphp.com/en/latest/reference/models-advanced.html

$di->set('otherDb', function() {});
$di->set('dbSlave', function() {});
$di->set('dbMaster', function() {});
class Exmodel extends \Phalcon\Mvc\Model
{
public function initialize()
{
$this->setConnectionService("otherDb");
}
}
class Exmodel extends \Phalcon\Mvc\Model
{
public function initialize()
{
$this->setReadConnectionService("dbSlave");
$this->setWriteConnectionService("dbMaster");
}
}

https://github.com/farwish/alcon/blob/master/src/Traits/ModelTrait.php

https://github.com/farwish/alcon/blob/master/src/Traits/ModelAdvanceTrait.php

[ dev-tools ]

#example:

phalcon model --name=question_log --namespace=Demo\\Frontend\\Models --directory=/home/www/Demo --output=apps/frontend/models

phalcon controller --name=User --namespace=Demo\\Frontend\\Controllers --directory=/home/www/ --output=apps/frontend/controllers --base-class=ControllerBase --force

[ cookies ]

// This method does not removes cookies from the _COOKIE superglobal
// This method overrides any cookie set before with the same name
$this->cookies->get('name')->delete(); // Sets a cookie to be sent at the end of the request
$this->cookies->set('name', 'value', time() + $expire, '/', false, 'example.com', true); // Sends the cookies to the client.
// Cookies aren't sent if headers are sent in the current request
$this->cookies->send(); // Check if a cookie is defined in the bag or exists in the _COOKIE superglobal
if ( $this->cookies->has('name') ) { // Get the cookie
$this->cookies->get('name'); // Get the cookie's value
$this->cookies->get('name')->getValue();
} // By default, cookies are automatically encrypted before being sent to the client and are decrypted when retrieved from the user. // Disable encryption in the following way
$di->set('cookies', function() {
return (new \Phalcon\Http\Response\Cookies())->useEncryption(false);
}); // To use encryption, a global key must be set.
$di->set('crypt', function() {
return (new \Phalcon\Crypt())->setKey('^YourOwnKey$');
});

通过查询手册可以获得上述相关内容的帮助。

推荐一个用于[Phalcon]项目开发的公用库:

https://github.com/farwish/alcon

Link: http://www.cnblogs.com/farwish/p/6357845.html

[PHP] Phalcon操作示范的更多相关文章

  1. RHEL6.3卸载OpenJDK操作示范:

    安装好的CentOS会自带OpenJdk,用命令 java -version ,会有下面的信息: java version "1.6.0" OpenJDK Runtime Envi ...

  2. Hadoop记录-hive操作示范

  3. [c++] STL = Standard Template Library

    How many people give up, because of YOU. Continue... 先实践,最后需要总结. 1. 数据流中的数据按照一定的格式<T>提取 ------ ...

  4. Scalaz(26)- Lens: 函数式不可变对象数据操作方式

    scala中的case class是一种特殊的对象:由编译器(compiler)自动生成字段的getter和setter.如下面的例子: case class City(name:String, pr ...

  5. 泛函编程(21)-泛函数据类型-Monoid

    Monoid是数学范畴理论(category theory)中的一个特殊范畴(category).不过我并没有打算花时间从范畴理论的角度去介绍Monoid,而是希望从一个程序员的角度去分析Monoid ...

  6. Excel2007条件格式怎么用

    Excel2007的条件格式功能十分的强大实用,较2003版改进十分的大,下面我们以经验记录为例做一简单的操作示范.注意前部分有二点技巧可借鉴,即不规则选取和不规则统一填充. 工具/原料 EXCEL2 ...

  7. Sqlite在Windows、Linux 和 Mac OS X 上的安装过程

    一:在 Windows 上安装 SQLite 1,下载 请访问SQLite下载页面http://www.sqlite.org/download.html,从Windows 区下载预编译的二进制文件.需 ...

  8. WPF:保存窗口当前状态截图方法

    在制作软件使用手册或者操作示范市,比较常用方式有截图和视频制作.如果软件内置当前状态的截图和操作视频的导出功能,则将极大简化这方面的工作.使用wpf编写的UI界面,截图的导出功能逻辑相对简单,通用的实 ...

  9. Mac OS X Mavericks使用手册

    基本信息 作者: 施威铭研究室 出版社:清华大学出版社 ISBN:9787302386018 上架时间:2014-12-30 出版日期:2015 年1月 开本:16 版次:1-1 所属分类: 计算机 ...

随机推荐

  1. JavaScript基础知识(if、if else、else if、while、switch...case语句)

    13.语句 概念:就是分号(:) 代表一条语句的结束 习惯:一行只编写一条语句:一行编写多条语句(代码可读性较差) 语句块:可以包含多条语句     "{ }"将多条语句包裹 u ...

  2. Android基础_BroadcastReceiver

    一:什么是BroadcastReceiver Broadcast(广播)是一种广泛运用于在应用程序之间一步传播消息的机制系统消息Android系统发出的,电池不足.来电信息等自定义消息第三方应用发出的 ...

  3. linux连接mysql 出现Access denied for user 'root'@'localhost'(using password: YES)错误解决方案

    linux连接mysql /usr/local/mysql/bin/mysql -uroot -p 输入密码出现Access denied for user 'root'@'localhost'(us ...

  4. WCF(一):初识WCF

    目录: 一.什么是WCF 二.WCF能做什么 三.WCF的模型 四.WCF的基本概念 五.WCF的快速创建 1.WCF是什么 A.WindowsCommunication Foundation(WCF ...

  5. 51 Nod 1027 大数乘法【Java大数乱搞】

    1027 大数乘法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 给出2个大整数A,B,计算A*B的结果. Input 第1行:大数A 第2行:大数B (A,B的长度  ...

  6. [51nod1709]复杂度分析

    给出一棵n个点的树(以1号点为根),定义dep[i]为点i到根路径上点的个数.众所周知,树上最近公共祖先问题可以用倍增算法解决.现在我们需要算出这个算法精确的复杂度.我们定义计算点i和点j最近公共组先 ...

  7. [bzoj1914] [Usaco2010 OPen]Triangle Counting 数三角形

    跑去看了黄学长的题解.. 第一次听说级角排序= =因为一直见计算几何就跑= = 级角排序就是按 原点和点连起来的边 与x轴正半轴构成的角的角度 排序...排序完效果就是逆时针旋转地枚举每个点. 要求的 ...

  8. Oracle database

    //下面这个通常直选择TCP就好了 此处的全局数据库根据实际情况来确定,如果是第一次,要和第一次一致.(见上面的图中的全局数据库) //这个可以使  计算机名(计算机—>属性).也可以是ip地址 ...

  9. Spring 数据库连接(Connection)绑定线程(Thread)的实现

    最近在看spring事务的时候在想一个问题:spring中的很多bean都是单例的,是非状态的,而数据库连接是一种有状态的对象,所以spring一定在创建出connection之后在threadloc ...

  10. Centos7 开放防火墙端口命令

    Centos 7 使用firewalld代替了原来的iptables,使用方法如下: >>>关闭防火墙 systemctl stop firewalld.service       ...