IMO它是一步一步地过渡到OOP方法绝对有效。
你的问题:
是的,你可以使用雄辩单机。
这里是packagist网站:https://packagist.org/packages/illuminate/database 添加"illuminate/database": "5.0.*@dev"
您composer.json
和运行composer update
。 现在你需要引导雄辩。 (https://github.com/illuminate/database)
以下是从回购的自述复制:
使用说明
首先,创建一个新的“胶囊”管理器实例。 Capsule旨在尽可能简化在Laravel框架之外配置库的使用。
use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
$capsule->addConnection([
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
]);
// Set the event dispatcher used by Eloquent models... (optional)
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
$capsule->setEventDispatcher(new Dispatcher(new Container));
// Set the cache manager instance used by connections... (optional)
$capsule->setCacheManager(...);
// Make this Capsule instance available globally via static methods... (optional)
$capsule->setAsGlobal();
// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
$capsule->bootEloquent();
一旦Capsule实例已被注册。你可以使用它像这样:
使用查询生成器
$users = Capsule::table('users')->where('votes', '>', 100)->get();
其他核心方法可以直接从胶囊中的相同方式从DB门面访问:
$results = Capsule::select('select * from users where id = ?', array(1));
使用架构生成器
Capsule::schema()->create('users', function($table)
{
$table->increments('id');
$table->string('email')->unique();
$table->timestamps();
});
使用雄辩ORM
class User extends Illuminate\Database\Eloquent\Model {}
$users = User::where('votes', '>', 1)->get();
有关使用此库提供的各种数据库工具的更多文档,请参阅Laravel框架文档。
感谢详细的答复..我想知道,如果我们能够创建)设置为架构的别名胶囊::架构(,因此它可以使用同样的方法,Laravel即模式::型号 - >行动()或Schema :: table() - > action()? – rolfk 2015-04-08 09:24:52
您可以创建一个Schema类并使用__callStatic将所有静态方法调用重定向到Capsule :: schema() - > $ method'。 – 2015-07-21 08:31:24