• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[PHP]Phalcon操作示范

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

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

 

Refer:Phalcon是鸡肋吗

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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
PHP正则匹配字符串中的中文发布时间:2022-07-10
下一篇:
php实现打印功能发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap