[PHP] Phalcon Operation Demonstration

Source: Internet
Author: User
Tags bulk insert crypt traits

This article will demonstrate the following actions:

Insert, Select, Update, calculation, Transaction, models Advanced, dev-tools, cookies

[Insert]

(1)

// in-model operations, data is a one-dimensional array of [' field ' = = ' value '].  $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 ), //  An array of values for the corresponding field in the order, and cannot contain an empty string  array_keys  ( $data ) //  field array   ); return   $db ->lastinsertid (); 

(3)

//BULK 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)

// in-model operations, parameters are optional. $arr = [    //  "conditions" + "question_id = 1"//"status" in the form of direct write conditions below    = 1 and role_id = 3 ",    " columns "=" Id,isdel ",    " order "=" sort DESC, Addtime DESC ",    " Limit "=" 2 ", ]; $all _res static:: Find ($arr//  multiple $first _resstatic:: FindFirst ($arr//   Single

(2)

// Phalcon Query Language $manager = \phalcon\di::getdefault ()->getshared (' Modelsmanager '); $phql __class__ . "WHERE question_id =: question_id:"; $res $manager->executequery ($phql$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 '],       //  field Array     [Time ()],              //  value array, corresponding order    "id = ' {$answer _id} '"  //  conditions ); 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 __class__ . "SET Iscollect =: Iscollect:where id = ' {$id} '"; // $data = [' Iscollect ' + 1]; $res $manager->executequery ($phql$data); $bool $res-Success (); return $bool;

(4)

// within the model $this->save ($data);

[Calculation]

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

//sumreturn Static::sum ([' Conditions ' = ' xxxx ', ' column ' = ' shownum ',]);//Averagereturn Static::average ([' Column ' = ' shownum ',]);//Maximumreturn Static::Maximum ([' Column ' = ' shownum ',]);//Minimumreturn Static::minimum ([' Column ' = ' shownum ',]);//quantity//conditionsreturn Static::Count("Area= ' Beijing '");//distinctreturn 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 function () {}); $di function () {}); $di function () {});
class extends \phalcon\mvc\model{    publicfunction  Initialize ()    {         $this->setconnectionservice ("Otherdb");}    }
class 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]

--name=question_log--namespace=demo\\frontend\\models--directory=/home/www/demo--output=apps/frontend/  --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 being 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 is 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, the cookie is automatically encrypted before being sent to the client and is decrypted when retrieved from The user. Disable encryption in the following$di->set (' Cookies ',function() {    return(New\phalcon\http\response\cookies ())->useencryption (false);});//to use encryption, a global key must is set.$di->set (' Crypt ',function() {    return(New\phalcon\Crypt())->setkey (' ^yourownkey$ ');});

Assistance with the above-mentioned content can be obtained by querying the manual.

Focus, a common library for [Phalcon] project development is recommended:

Https://github.com/farwish/alcon

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

@ Black eyed poet <www.farwish.com>

[PHP] Phalcon Operation Demonstration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.