26/09
2017
Wallogit.com
2017 © Pedro Peláez
A simple Pdo library for micro PHP framework
A simple Pdo library for micro PHP framework.
It can be done for CRUD. It support write and read are separation., (*1)
composer require biaoqianwo/simple-pdo, (*2)
use \Bee\PDO\Model; Model::config(require_once __DIR__ . '/app/config/db.php');
The config file db.php like:, (*3)
return [
'host' => '127.0.0.1',
'port' => 3306,
'dbname' => 'mysql_test',
'options' => null,
'username' => 'root',
'password' => '',
];
If your write and read database is different.You can config like:, (*4)
return [
'read' => [
[
'host' => '127.0.0.1',
'port' => 3307,
'dbname' => 'mysql_test',
'options' => null,
'username' => 'root',
'password' => '',
],
[
'host' => '127.0.0.1',
'port' => 3308,
'dbname' => 'mysql_test',
'options' => null,
'username' => 'root',
'password' => '',
]
],
'write' => [
[
'host' => '127.0.0.1',
'port' => 3306,
'dbname' => 'mysql_test',
'options' => null,
'username' => 'root',
'password' => '',
]
]
];
Please note that the write and read are existing meanly. If you use write,you must use read. The write and read can be same.
可以读写分离,只是必须读和写的节点必须同时存在,可以相同。, (*5)
The model should and only inherit Bee\PDO\Model. You do not need to code anything more. So it is simple., (*6)
namespace App;
use Bee\PDO\Model;
/**
* Class User
* @package App
*/
class User extends Model
{
}
And then your controller can use the user model. You can see the demo/UserController.php. e.g., (*7)
$sql = "select * from users where id = ?"; $conditions = [$id]; $result = User::first($sql, $conditions); var_dump($result);