1 原理
使用注解来完成ORM整合
将Core\init\MyDB的实例注入BeanFactory,
Core\init\MyDB实例里有一个LvDB的实例,装载了ORM
已经装载对象
self::$container->get('Core\init\MyDB')
-----Core\init\MyDB Object ( [lvDB] => Illuminate\Database\Capsule\Manager Object ( [manager:protected] => Illuminate\Database\DatabaseManager Object ( [app:protected] => Illuminate\Container\Container Object ( [resolved:protected] => Array ( ) [bindings:protected] => Array ( ) [methodBindings:protected] => Array ( ) [instances:protected] => Array ( [config] => Illuminate\Support\Fluent Object ( [attributes:protected] => Array ( [database.fetch] => 5 [database.default] => default [database.connections] => Array ( [default] => Array ( [driver] => mysql [host] => 10.10.10.234 [port] => 12345 [database] => seckill [username] => root [password] => 123456 [charset] => utf8mb4 [collation] => utf8mb4_general_ci [prefix] => ) [test2] => Array ( [driver] => mysql [host] => localhost [database] => test2 [username] => root [password] => 500500 [charset] => utf8 [collation] => utf8_unicode_ci [prefix] => ) ) ) ) ) [scopedInstances:protected] => Array ( ) [aliases:protected] => Array ( ) [abstractAliases:protected] => Array ( ) [extenders:protected] => Array ( ) [tags:protected] => Array ( ) [buildStack:protected] => Array ( ) [with:protected] => Array ( ) [contextual] => Array ( ) [reboundCallbacks:protected] => Array ( ) [globalBeforeResolvingCallbacks:protected] => Array ( ) [globalResolvingCallbacks:protected] => Array ( ) [globalAfterResolvingCallbacks:protected] => Array ( ) [beforeResolvingCallbacks:protected] => Array ( ) [resolvingCallbacks:protected] => Array ( ) [afterResolvingCallbacks:protected] => Array ( ) ) [factory:protected] => Illuminate\Database\Connectors\ConnectionFactory Object ( [container:protected] => Illuminate\Container\Container Object ( [resolved:protected] => Array ( ) [bindings:protected] => Array ( ) [methodBindings:protected] => Array ( ) [instances:protected] => Array ( [config] => Illuminate\Support\Fluent Object ( [attributes:protected] => Array ( [database.fetch] => 5 [database.default] => default [database.connections] => Array ( [default] => Array ( [driver] => mysql [host] => 10.10.10.234 [port] => 12345 [database] => seckill [username] => root [password] => 123456 [charset] => utf8mb4 [collation] => utf8mb4_general_ci [prefix] => ) [test2] => Array ( [driver] => mysql [host] => localhost [database] => test2 [username] => root [password] => 500500 [charset] => utf8 [collation] => utf8_unicode_ci [prefix] => ) ) ) ) ) [scopedInstances:protected] => Array ( ) [aliases:protected] => Array ( ) [abstractAliases:protected] => Array ( ) [extenders:protected] => Array ( ) [tags:protected] => Array ( ) [buildStack:protected] => Array ( ) [with:protected] => Array ( ) [contextual] => Array ( ) [reboundCallbacks:protected] => Array ( ) [globalBeforeResolvingCallbacks:protected] => Array ( ) [globalResolvingCallbacks:protected] => Array ( ) [globalAfterResolvingCallbacks:protected] => Array ( ) [beforeResolvingCallbacks:protected] => Array ( ) [resolvingCallbacks:protected] => Array ( ) [afterResolvingCallbacks:protected] => Array ( ) ) ) [connections:protected] => Array ( ) [extensions:protected] => Array ( ) [reconnector:protected] => Closure Object ( [this] => Illuminate\Database\DatabaseManager Object *RECURSION* [parameter] => Array ( [$connection] => <required> ) ) [doctrineTypes:protected] => Array ( ) ) [container:protected] => Illuminate\Container\Container Object ( [resolved:protected] => Array ( ) [bindings:protected] => Array ( ) [methodBindings:protected] => Array ( ) [instances:protected] => Array ( [config] => Illuminate\Support\Fluent Object ( [attributes:protected] => Array ( [database.fetch] => 5 [database.default] => default [database.connections] => Array ( [default] => Array ( [driver] => mysql [host] => 10.10.10.234 [port] => 12345 [database] => seckill [username] => root [password] => 123456 [charset] => utf8mb4 [collation] => utf8mb4_general_ci [prefix] => ) [test2] => Array ( [driver] => mysql [host] => localhost [database] => test2 [username] => root [password] => 500500 [charset] => utf8 [collation] => utf8_unicode_ci [prefix] => ) ) ) ) ) [scopedInstances:protected] => Array ( ) [aliases:protected] => Array ( ) [abstractAliases:protected] => Array ( ) [extenders:protected] => Array ( ) [tags:protected] => Array ( ) [buildStack:protected] => Array ( ) [with:protected] => Array ( ) [contextual] => Array ( ) [reboundCallbacks:protected] => Array ( ) [globalBeforeResolvingCallbacks:protected] => Array ( ) [globalResolvingCallbacks:protected] => Array ( ) [globalAfterResolvingCallbacks:protected] => Array ( ) [beforeResolvingCallbacks:protected] => Array ( ) [resolvingCallbacks:protected] => Array ( ) [afterResolvingCallbacks:protected] => Array ( ) ) ) [dbSource:Core\init\MyDB:private] => default )
2 代码
2.1 \pro\core\annotationhandlers\DBHandler.php
<?php namespace Core\annotationhandlers; use Core\annotations\DB; use Core\BeanFactory; use Core\init\MyDB; return [ DB::class=>function(\ReflectionProperty $prop,$instance,$self) { $mydbBean = BeanFactory::getBean(MyDB::class); //从新获取一个对象 $mydbBean ->setDbSource($self->source);//新MyDB对象设置数据源 $prop->setAccessible(true); // $prop->setValue($instance,$mydbBean); return $instance; } ];
2.1 \seckill\pro\core\annotations\DB.php
<?php namespace Core\annotations; use Doctrine\Common\Annotations\Annotation\Target; /** * @Annotation * @Target({"PROPERTY"}) */ class DB { public $source = 'default'; }
2.3 \seckill\pro\app\config\db.php
<?php return [ "default"=>[ 'driver' => 'mysql', 'host' => '10.10.10.234', //我的dicker上的 da9aad4acad8 mysql:5.6 'port' => '12345', 'database' => 'seckill', 'username' => 'root', 'password' => '123456', 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_general_ci', 'prefix' => '', ] , 'test2' =>[ 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'test2', 'username' => 'root', 'password' => '500500', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ] ];
2.4 \seckill\pro\app\controllers\UserController.php
use Core\annotations\DB; use Core\init\MyDB;
加载DB
/** * @DB * @var MyDB */ private $db;
使用
/** * @RequestMapping(value="/test") */ public function test(Response $response ) { return $this->db->table("users")->get(); }