1. Database Configuration applications/xxx/config/db.php
<?phpNamespaceConfig;/** * MySQL configuration *@author Walkor * *Classdb{/** * An instance configuration of the database, use the following as follows * $user _array = db::instance (' user ')->select (' Name,age ')->from (' users ')->where (' age>12 ')->query (); * Equivalent to * $user _array = db::instance (' user ')->query (' SELECT ' name ', ' age ' from ' users ' WHERE ' the ' Age ' >12 '); *@var Array * /public static $user = Array ( ' host ' = ' 127.0.0.1 ' , ' port ' = 330 6, ' user ' = ' your_user_name ', ' password ' = ' your_password ', ' dbname ' = ' user ', ' charset ' = ' utf8 ',);}
2, applications/xxx/event.php
<?phpUse \Gatewayworker\Lib\Gateway;Use \Gatewayworker\Lib\Db;/** * Database example, suppose there is a user library with a user table */Classevent{/** * Trigger This method when there is a message, print 2 user information according to the command sent *The client_id of the _id message @param int $client *@param string $message Message *@return void */PublicStaticfunctionOnMessage($client _id,$message) {Message sent$commend = Trim ($message);if ($commend!== ' get_user_list ') {gateway::sendtoclient ($client _id, "Unknown commend\n"); return;} //Get a list of users (here is a temporary test database) $ret = db::instance ('user ')->select ('* ')->from ('users ') Where (' uid>3 ')->offset (5)->limit (2)->query (); //Print result return gateway::sendtoclient ($client _id, Var_export ($ret, True));}}
Some sample configurations used by database classes
Configure database information in config/db.php, if you have multiple databases, you can configure multiple instances in db.php for example, two DB instances are configured below
<?phpNamespaceConfig;Classdb{DB Instance 1PublicStatic$db 1 =Array' Host ' =' 127.0.0.1 ',' Port ' =3306,' User ' =' Mysql_user ',' Password ' = ' dbname ' = DB1 ', ' charset ' + ' UTF8 ',); //db instance 2 public static $db 2 = array ( ' host ' = 127.0.0.1 ', ' port ' = 3306, ' user ' = ' Mysql_user ', ' password ' = ' Mysql_password ', ' dbname ' = ' DB2 ', Span class= "hljs-string" > ' charset ' + ' UTF8 ',);}
How to use
Use \Gatewayworker\Lib\Db;$db 1 = db::instance (' DB1 ');$db 2 = db::instance (' DB2 ');Get all data$db 1->select (' Id,sex ')->from (' Persons ')->where (' sex=: Sex ')->bindvalues (Array' Sex ' =' M '))->query ();Equivalent to$db 1->select (' Id,sex ')->from (' Persons ')->where ("Sex= ' F '")->query ();Equivalent to$db 1->query ("Select Id,sex from ' Persons ' WHERE sex= ' M '");Get a row of data$db 1->select (' Id,sex ')->from (' Persons ')->where (' sex=: Sex ')->bindvalues (Array' Sex ' =' M '))->row ();Equivalent to$db 1->select (' Id,sex ')->from (' Persons ')->where ("Sex= ' F '")->row ();Equivalent to$db 1->row ("Select Id,sex from ' Persons ' WHERE sex= ' M '");Get a column of data$db 1->select (' ID ')->from (' Persons ')->where (' sex=: Sex ')->bindvalues (Array' Sex ' =' M '))->column ();Equivalent to$db 1->select (' ID ')->from (' Persons ')->where ("Sex= ' F '")->column ();Equivalent to$db 1->column ("Select ' ID ' from ' Persons ' WHERE sex= ' M '");Get a single value$db 1->select (' Id,sex ')->from (' Persons ')->where (' sex=: Sex ')->bindvalues (Array' Sex ' =' M '))->single ();Equivalent to$db 1->select (' Id,sex ')->from (' Persons ')->where ("Sex= ' F '")->single ();Equivalent to$db 1->single ("Select Id,sex from ' Persons ' WHERE sex= ' M '");Complex queries$db 1->select (' * ')->from (' table1 ')->innerjoin (' Table2 ',' Table1.uid = Table2.uid ')->where (' Age >: Age ')->groupby (Array' aid '))->having (' foo= ' foo ')->orderby (Array' Did ')->limit (Ten)->offset ()->bindvalues (Array (' Age ' =13));Equivalent to$db 1->query (SELECT * from ' table1 ' INNER joins ' table2 ' on ' table1 '. ' uid ' = ' table2 '. ' UID ' WHERE the Age >GROUP by aid have foo="Foo" ORDER by did LIMITTen OFFSET20 ");Insert$insert _id =$db 1->insert (' Persons ')->cols (Array' Firstname ' =' ABC ',' Lastname ' =' EFG ',' Sex ' =' M ',' Age ' =)->query (); equivalent to$insert _id =$db 1->query ("INSERT into ' Persons ' (' Firstname ', ' Lastname ', ' Sex ', ' age ') VALUES (' abc ', ' EFG ', ' M ', 13)");Update$row _count =$db 1->update (' Persons ')->cols (Array' Sex '))->where (' Id=1 ')->bindvalue (' Sex ',' F ')->query ();Equivalent to$row _count = $db 1->update ( ' Persons ')->cols ( array ( ' sex ' => ' F '))->where ( ' id=1 ')->query (); //equivalent to $row _count = $db 1-> Query ( "UPDATE ' Persons ' SET ' sex ' = ' F ' WHERE id=1"); $row _count = $db 1->delete ( ' Persons ')->where ( ' id=9 ')->query (); Equivalent to $row _count = $db 1->query (
Gateway/worker Model Database Use example