Pull to refresh

PHP Microservice Framework Swoft: Use Database Part 1

Reading time2 min
Views1.1K


This article we are going to learn is: How to install and run the Swoft database.


This article is one of a series of articles on the Swoft Database ORM. Let's learn about Swoft!

What is Swoft?


Swoft is a PHP high performance microservice coroutine framework. It has been published for many years and has become the best choice for php.


It can be like Go, built-in coroutine web server and common coroutine client and is resident in memory, independent of traditional PHP-FPM.


There are similar Go language operations, similar to the Spring Cloud framework flexible annotations.


Through three years of accumulation and direction exploration, Swoft has made Swoft the Spring Cloud in the PHP world, which is the best choice for PHP's high-performance framework and microservices management.


Github



Install


Here we use composer to install the db component


composer require swoft/db

The Swoft database is based on the PDO extension driver, please make sure there are mysqld and PDO extensions.


Config


The configuration of the database is placed in the app\bean.php file, and you can think of the configured db as a bean object.


return [
    'db'         => [
       'class'     => Database::class,
       'dsn'       => 'mysql:dbname=test;host=127.0.0.1',
       'username'  => 'root',
       'password'  => '123456',
       'charset'   => 'utf8mb4',
    ],
];

The configuration is similar to the configuration of the yii2 object attribute injection method,You can get the currently configured Database object with \bean('db')


  • class Specify which class to use for the current bean container. Of course, you can also specify your own implementation of the database class.
  • dsn Connection configuration information that PDO needs to use
  • username Data login username
  • password Database login password
  • charset Database character set

If you want to configure master-slave/cluster, please refer toSwoft Database 文档


Simple to use


Select


class UserController {

    /**
     * Displays a list of all users of the application
     *
     * @return array
     */
    public function index()
    {
        $users = DB::select('select * from users where active = ?', [1]);

        return (array)users;
    }
}

The select method will always return an array, and each result in the array is an array, and the result value can be accessed like this:


foreach ($users as $user) {
    echo $user['name'];
}

Transaction


You can use DB's transaction method to run a set of operations in a database transaction. If an exception occurs in the closing closure of the transaction, the transaction will be rolled back. If the transaction closure Closure is executed successfully, the transaction will be committed automatically. Once you've used transaction, you no longer need to worry about manual rollback or commit issues:


DB::transaction(function () {
    DB::table('users')->update(['status' => 1]);

    DB::table('posts')->delete();
});

Github


Tags:
Hubs:
+1
Comments0

Articles

Change theme settings