1 安装
composer require illuminate/database
文档 https://laravelacademy.org/post/19521.html
https://laravelacademy.org/post/126.html
2 初步封装Laravel ORM库、基本查询、使用模型
seckill\pro\test4.php
方法一:原始查找 获取对象stdClass
<?php require_once __DIR__.'/vendor/autoload.php'; use Illuminate\Database\Capsule\Manager as DB; $database = [ 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'test', 'username' => 'root', 'password' => '123456', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ]; $db = new DB(); //创建连接 $db->addConnection($database); //设置全局静态访问 $db->setAsGlobal(); //启动Eloquent $db->bootEloquent(); $users = $db::table('users')->get(); foreach ($users as $user){ echo $user->name; }
方式二 使用模型
\seckill\pro\app\models\Users.php
<?php namespace App\models; use Illuminate\Database\Eloquent\Model; class Users extends Model { protected $table = 'users'; protected $primaryKey = 'id'; }
\seckill\pro\test4.php
$users = \App\models\Users::all(); foreach ($users as $user){ echo $user->name; }
3 初步封装Laravel ORM 、支持自定义配置、支持代码提示
3.1 封装
l\pro\app\config\db.php 配置文件
<?php return [ "default"=>[ 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'test', 'username' => 'root', 'password' => '123456', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ] ];
l\pro\core\init\DB.php 封装
<?php namespace Core\init; use Core\annotations\Bean; use Illuminate\Database\Capsule\Manager as lvDB; class DB { private $lvDB; public function __construct() { //PHP默认定义了一些“超级全局(Superglobals)”变量,这些变量自动全局化,而且能够在程序的任何地方中调用,比如$_GET和$ _REQUEST等等。它们通常都来自数据或者其他外部数据,使用这些变量通常是不会产生问题的,因为他们基本上是不可写的。 // //但是你可以使用你自己的全局变量。 // //使用关键字“global”你就可以把全局数据导入到一个函数的局部范围内。 global $GLOBAL_CONFIGD; //default 为默认数据源 if (isset($GLOBAL_CONFIGD['db']) && isset($GLOBAL_CONFIGD['db']['default'])){ $this->lvDB = new LvDb(); //创建连接 $this->lvDB->addConnection($GLOBAL_CONFIGD['db']['default']); //设置全局静态访问 $this->lvDB->setAsGlobal(); //启动Eloquent $this->lvDB->bootEloquent(); } } //__call(),这个方法用来监视一个对象中的其它方法。 ////如果你试着调用一个对象中不存在或被权限控制中的方法, //__call 方法将会被自动调用。 public function __call($methodName, $arguments) { // TODO: Implement __call() method. //用。。。解构数组 return $this->lvDB::$methodName(...$arguments); } }
使用 \pro\test4.php
<?php require_once __DIR__.'/vendor/autoload.php'; require_once __DIR__."/app/config/define.php"; $db = new \Core\init\DB(); $users = $db->table('users')->get(); foreach ($users as $user){ echo $user->name; }
3.2 支持代码提示
\pro\core\init\DB.php 加上注释就好了
/** * Class DB * * @package Core\init * @method \Illuminate\Database\Query\Builder table(string $table,string|null $as=null,string|null $connection=null) */ class DB
table方法的三个参数是从这里来的
效果如下
4 让db支持更多的数据源
4.1 添加配置
D:\seckill\pro\app\config\db.php
<?php return [ "default"=>[ 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'test', 'username' => 'root', 'password' => '500500', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ] , 'test2' =>[ 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'test2', 'username' => 'root', 'password' => '500500', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ] ];
4.2 添加连接
D:\seckill\pro\core\init\DB.php
$this->lvDB->addConnection($GLOBAL_CONFIGD['db']['default']); $this->lvDB->addConnection($GLOBAL_CONFIGD['db']['test2'],'test2');
4.3 使用
D:\seckill\pro\test4.php
$users = $db->connection('test2')->table('users','u')->get();
或者
$users = $db->table('users','u','test2')->get();