Several common SQL processing used in Iwebshop framework

Source: Internet
Author: User

Query class tags (for front-end traversal)

Instance:

{Query:name=goods}
{$item [' name ']}<br/>
{/query}

I write code application in actual development :

<php//The following code is placed on the HTML page of {query:name=user fields=username where=id eq $user _id} {$item [' username ']} {/query} for Shopping Checklist </td>? >


Paging:

{set: $page =ireq::get (' page ') ==null?1:ireq::get (' page ');} Defining Paging variables
{query:name=goods page= $page pagesize=5}
{$key}:{$item [' Name ']}<br/>
{/query}
{$query->getpagebar ()}//Calling paging method


Database reads and writes:


IQuery class:

IQuery provides a wealth of properties and functions, through a simple property configuration, you can implement a lot of SQL query functions, such as we want to query the vendor
Product List (goods) inside the id=10 of the goods, then:
$goodsDB = new IQuery (' goods ');
$goodsDB->where = "id = 10";
$goodsData = $goodsDB->find ();
. Find () is an interface that executes the final data of the query.

After you have created the IQuery object, you can set various class properties to combine the query data.
The main attribute list is as follows:
Property name Data type description
Fields String Optional Default * Read field data from table
Where String optional default no query condition
Join String optional Default no table join operation, such as:
$db = new IQuery (' Goods as G ');
$db->join= "left join products as P on g.id = p.goods_id";
$db->find ();
Group String can optionally default no table grouping operations, such as:
$db = new IQuery (' Goods as G ');
$db->group = "Price";
$db->find ();
Having String optional default filter for no table grouping results, setting the group property makes sense
Order String Optional Default no table sort field
Limit Int Optional default 20 bar reads a specified number of bars
Page Int Optional default paging is not set. After setting the IQuery class library, add the paging property (pagination Class)
pagesize Int Optional Default 20 The amount of data displayed per pagination, setting the page property makes sense
Pagelength Int Optional Default 10 page shows how many pages, setting page property makes sense
Cache String optional Memcache, caching query results to improve efficiency and reduce database pressure. Fill in the cached mode, provided your system must support Iwebshop caching technology

Debug Int optional default 0 off; 1 open Debug SQL statement, the system will automatically output the full SQL original ecological statement


I write code application in actual development

Inquire:

function Give_goods_list () {$giveaway _obj = new IQuery (' giveaway ');   $giveaway _obj, where = "Is_del = 0";   $giveaway _infos = $giveaway _obj, find ();   $this->data = $giveaway _infos; $this->redirect (' give_goods_list ');}


Imodel class:

Database writes generally with the Imodel class, currently supports update (update), add (addition), Del (delete), droptable (Uninstall
Table), CreateTable (CREATE table).
The general use is to create a DB instance by creating a new IModel (table name) object, and then call the SetData () interface for the data set
Reset
For example, to update the Price field of the goods table, it is:
$goodsDB = new IModel (' goods ');
$goodsDB->setdata (Array (' price ' = 1000));
$goodsDB->update (' id = 2 ');


Method name Data type description
Update ($where,
$except =array ()
)
$where: String Update condition
$except: array-specific expressions
Field (non-string type)
Update record
Through $this->setdata ($array); $array (field = Update data)
Table update fields correspond to data relationships
Add () adds a record
Through $this->setdata ($array); $array (field = + Add data)
Table add record corresponding data relation
Del ($where) $where: string Delete condition Delete Record
Delete records that meet $where conditions

Droptable () Unload table
Delete the table that corresponds to the current Imodel instance
CreateTable () CREATE table
Create a table for the current Imodel instance
Through $this->setdata ($array); Set CREATE TABLE Element (multidimensional array)
$array = Array (
"column" = = Field Configuration Array ("type" = = data type, "default"
= = default value, "comment" = = Field Comment, "auto_increment" =
Value self-growth)
"Comment" = table notes
The index = table indexes the array (index class
Type: Primary,key,unique "=" Field name ")

I write code application in actual development :

Add to:

Function supplier_edit_add () {    $id  = ifilter::act (IReq::get (' id '), ' int ');     $company    = ifilter::act (ireq::get (' Firm_name '));    $address  = ifilter::act (Ireq::get (' address '));    $pname   = ifilter::act (IReq:: Get (' name '));    $mobile  = ifilter::act (Ireq::get (' Mobile '));    $phone   = ifilter::act (Ireq::get (' telephone '));    $img  = ifilter::act (IReq:: Get (' img '));    $account   = ifilter::act (ireq::get (' Account_num '));         $bank    = ifilter::act (ireq::get (' Bank_name '));     $province  = ifilter::act (Ireq::get (' Province '), ' int ');    $city      = ifilter::act (Ireq::get (' City '), ' int ');    $area       = ifilter::act (IReq::get (' area '), ' int. ');    $_post[' area '] =  ";    if ($province   &&  $city  &&  $area)    {      $_post [' Area '] = array ($province, $city, $area);   }    $user _id = $this- >admin[' admin_id '];    $stock   = new imodel (' supplier_list ');     $stock _init =  array (       ' Firm_name '      =>    $company,       ' area '           =>   $_post[' area '] ?  ",". Join (",", $_post[' area '). ","  :  ",        ' address '       =>     $address,       ' name '           =>    $pname,       ' mobile '        = >    $mobile,       ' telephone '     =>     $phone,       ' img '            =>    $img,       ' Account_num '   = >   $account,       ' bank_name '     =>    $bank,        ' create_time '   =>   date (' Y-m-d h:i:s ', Time ()),        ' admin_id '      = >    $user _id   )    if ($id) {        $stock->setdata ($stock _init);       $stock->update (' id =  '). $ID);    }else{       $stock->setdata ($stock _init);        $ Stock->add ();   }    $this->redirect (' supplier_list ');} Delete: (Note: Soft Delete, database retention) Function supplier_list_del () {    $id  = ifilter::act (IReq::get (' Id '), ' int ');    $stock  = new imodel (' supplier_list ');    $stock- >setdata (Array (' Is_del ' =>1));    if ($id)    {        $stock->update (Util::joinstr ($id));    }else{      die (' Please select the data to be deleted ');   }    $this->redirect ("Supplier_list");}

Update:

function Suppliet_list_restore () {$id = Ifilter::act (ireq::get (' id '), ' int ');   Generate Goods Object $stock = new IModel (' supplier_list ');   $stock->setdata (Array (' Is_del ' =>0));   if ($id) {$stock->update (util::joinstr ($id));   } else {die (' Please select data to recover '); } $this->redirect ("Supplier_list");}

The original SQL can be used Idbfactory::getdb ()->query ($sql); You can write the original SQL statement directly, but be aware
The table prefix is incremented in the SQL statement because Idbfactory does not have any modifications to the $sql and is delivered directly to MySQL as 100%
This method can be used by some special, complex SQL.

I write code application in actual development :

$sql _class = "Select zce.category_id,gs.brand_id,zc.parent_id from (zqwy_goods as GS-left join Zqwy_category_extend as Zce On gs.id = zce.goods_id) left join Zqwy_category as ZC on zce.category_id = zc.id WHERE gs.id = ". $v [' goods_id ']."; "; $goods _class = Idbfactory::getdb ()->query ($sql _class);


This article is from the "Kangjunfei" blog, make sure to keep this source http://kangjunfei.blog.51cto.com/11556647/1915580

Several common SQL processing used in Iwebshop framework

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.