首页 / PHP / [PHP] Phalcon操作示范
[PHP] Phalcon操作示范
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了[PHP] Phalcon操作示范,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5624字,纯文字阅读大概需要9分钟。
内容图文
这篇内容将对下列操作进行示范:
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(); returnreset($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‘, ]); // 平均值returnstatic::average([ ‘column‘ => ‘shownum‘, ]); // 最大returnstatic::maximum([ ‘column‘ => ‘shownum‘, ]); // 最小returnstatic::minimum([ ‘column‘ => ‘shownum‘, ]); // 数量 // conditionsreturnstatic::count("area=‘北京‘"); // distinctreturnstatic::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 { publicfunction initialize() { $this->setConnectionService("otherDb"); } }
class Exmodel extends \Phalcon\Mvc\Model { publicfunction 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 superglobalif ( $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
@黑眼诗人 <www.farwish.com>
原文:http://www.cnblogs.com/farwish/p/6357845.html
内容总结
以上是互联网集市为您收集整理的[PHP] Phalcon操作示范全部内容,希望文章能够帮你解决[PHP] Phalcon操作示范所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。