PHP Development Framework Laravel Database Operation method Summary, Framework laravel_php Tutorial

Source: Internet
Author: User
Tags event listener

PHP Development Framework Laravel Database Operation method Summary, framework Laravel


One, read/write connection

Sometimes you might want to use a database connection for a SELECT statement, and another for inserting, updating, and deleting statements. Laravel makes this breeze, will always use the correct connection whether using the original query, query Builder or eloquent ORM.

How the read/write connection should be configured, let's take a look at this example:
Copy the Code code as follows:
' MySQL ' = array (' read ' + = Array (' host ' = ' 192.168.1.1 '), ' write ' = = Array (' host ' = ' 196.168.1.2 '), ' Driver ' + ' mysql ', ' database ' = ' database ', ' username ' + ' root ', ' password ' + ', ' charset ' = ' utf8 ', ' Collation ' = ' utf8_unicode_ci ', ' prefix ' and ' = ')

Note that two keys are added to the configuration array: Read and write. These two keys have an array value that contains a key: the host. The rest of the read-write database option is merged from the primary MySQL connection to the array. So, we just need to put the items into the read and write array if we want to overwrite the values in the primary array. So, in this case, 192.168.1.1 will be used as a "read" connection, and while192.168.1.2 will be used as a "write" connection. Database credentials, prefixes, character sets, and all other options in the primary MySQL array will be connected across two shares.

Second, run the query

Once you have configured the database connection, you can run the query class using DB.

Run a Select query

Copy the Code code as follows:
$results = Db::select (' select * from users where id =? ', array (1));

The selection method of the result always returns an array.

Run an INSERT statement

Copy the Code code as follows:
Db::insert (' INSERT into users (ID, name) VALUES (?,?) ', Array (1, ' Dayle '));

Run an UPDATE statement

Copy the Code code as follows:
Db::update (' update users set votes = + WHERE name =? ', Array (' John ') ');

Run a DELETE statement

Copy the Code code as follows:
DB::d elete (' Delete from users ');

Note: The update and DELETE statements affect the number of rows returned by the action.

Run a general Declaration

Copy the Code code as follows:
Db::statement (' drop table users ');

Querying for event snooping

You can query the event listener using DB:: Listening method:

Copy the Code code as follows:
Db::listen (function ($sql, $bindings, $time) {//});

Third, database transactions

To run a set of operations on a database transaction, you can use the transaction method:
Copy the Code code as follows:
Db::transaction (function () {db::table (' users ')->update (Array (' votes ')
= 1)); Db::table (' posts ')->delete ();});

Note: Any exception thrown in the transaction will cause the automatic transaction to be rolled back

Sometimes you may need to start a transaction:

Copy the Code code as follows:
Db::begintransaction ();

You can roll back the transaction by rolling back the method:

Copy the Code code as follows:
Db::rollback ();

Finally, you can submit a transaction by submitting the method:

Copy the Code code as follows:
Db::commit ();

Iv. Access connections

When using multiple connections, you can access them through the DB:: Connection method:

Copy the Code code as follows:
$users = db::connection (' foo ')->select (...);

You can also access the original, potential PDO instances:
Copy the Code code as follows:
$pdo = Db::connection ()->getpdo ();

Sometimes you may need to reconnect to a given database:
Copy the Code code as follows:
Db::reconnect (' foo ');

If you need to disconnect from the given database will exceed the underlying PDO instance ' smax_connections limit, use the Disconnect method:
Copy the Code code as follows:
DB::d isconnect (' foo ');

V. Inquiry LOG

By default, the Laravel log saves all queries in memory to run the current request. However, in some cases, such as when the number of rows is inserted, this may cause the application to use extra memory. To disable logging, you can use the Disablequerylog method:

Copy the Code code as follows:
Db::connection ()->disablequerylog ();

O get a set of executed queries, you can use the Getquerylog method:
Copy the Code code as follows:
$queries = Db::getquerylog ();


Php_laravel Frame

280907494 Development Group, many of the group engaged in this.

Who has the PHP source framework (can read the database, with the background to add data)

You go to the next dedecms.

http://www.bkjia.com/PHPjc/874114.html www.bkjia.com true http://www.bkjia.com/PHPjc/874114.html techarticle PHP Development Framework Laravel Database Operation method Summary, framework Laravel One, read/write connection sometimes you might want to use a database connection for a SELECT statement, and another for inserting, updating, and ...

  • 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.