如何使用Illuminate Database Query Builder&在PHP 5.3.x的其他框架中雄辩 [英] How to use Illuminate Database Query Builder & Eloquent in other framework with PHP 5.3.x

查看:249
本文介绍了如何使用Illuminate Database Query Builder&在PHP 5.3.x的其他框架中雄辩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我过去曾与Laravel合作完成过几个项目,但现在我需要为项目做一些轻便的工作,并开始使用Slim,它可以很好地满足我的需求,我想要出色的Eloquent ORM和Laravel查询生成器,现在离不开它:) 现在,我设法利用Taylor在GitHub上显示的信息复制了他的代码,从而与作曲家一起使用了所有

I did a couple of projects with Laravel in the past but now I need something very light for a project and went to use Slim, it works great for what I need and I want the great Eloquent ORM and Query Builder of Laravel, can't go without it now :) Now I manage to get it all working with composer, using the information that Taylor displayed on his GitHub, copied his piece of code

use Illuminate\Database\Capsule\Manager as Capsule;

$capsule = new Capsule;

$capsule->addConnection([
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => '',
    'username'  => '',
    '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();

这对我的本地开发人员(PHP 5.4.x)来说效果很好,但是当我将其放在服务器 PHP 5.3.x 上时,它不再起作用了:(现在我看到1问题是我们不能使用像[]这样的匿名数组,而应该写为array()的旧方法,即在addConnection(array($settings))内部,现在再远一点……但是之后似乎在$capsule->setEventDispatcher()内部崩溃,并且我的服务器上没有日志(我只能在这里和那里通过var_dump()找到),它只是一个NAS,我什至不想花几个小时来找出答案如何启用它,但是有趣的是我有一个Laravel 4项目正在使用它……那么为什么仅仅构建它的一部分Illuminate\Database却不起作用呢? 我还发现了另一段代码使Eloquent ORM在PHP 5.3.x中工作

That works perfectly good on my local dev (PHP 5.4.x) but when I put it on my server PHP 5.3.x, it just doesn't work anymore :( Now I see 1 problem being that we can't use anonymous array like this [] but instead should be written the old way of array(), that is inside the addConnection(array($settings)) great, now a little further...but then after it seems to crash inside $capsule->setEventDispatcher() and I don't have logs on my server (I only found through var_dump() here and there), it's just a NAS and I don't want to even think of spending a few hours just to find out how to enable it. But what's funny is that I had a Laravel 4 project working with it... so why just building a part of it Illuminate\Database doesn't work then? I also found another piece of code to make Eloquent ORM work in PHP 5.3.x

$settings = array(
    'driver' => '',
    'host' => '127.0.0.1',
    'database' => '',
    'username' => '',
    'password' => '',
    'charset'   => "utf8",
    'collation' => 'utf8_general_ci',
    'prefix' => ''
);


// Bootstrap Eloquent ORM
$connFactory = new \Illuminate\Database\Connectors\ConnectionFactory(new Illuminate\Container\Container);
$conn = $connFactory->make($settings);
$resolver = new \Illuminate\Database\ConnectionResolver();
$resolver->addConnection('default', $conn);
$resolver->setDefaultConnection('default');
\Illuminate\Database\Eloquent\Model::setConnectionResolver($resolver);

但是如果我使用这段代码,顺便说一下,这对Models来说是很好的.我需要使用$conn->table('...')...代替我想要的DB::table(....)的Facade简单方法,您为什么这么说很重要?好吧,如果我将来想转换为Laravel,该怎么办...我必须将所有$conn->更改为DB::,所以我宁愿在第一时间就正确地做.如果有人知道如何在第二段代码上创建Facade,我也将很高兴...谢谢您的帮助.

but if I use this piece of code, which is good with Models by the way. I need to use $conn->table('...')... instead of the Facade simple way of DB::table(....) which is what I want, why is it important you would say? Well what if I want to convert to Laravel in the future... I would have to change all of the $conn-> to DB:: so I would rather do it right the first time. If someone knows how to create Facade on the 2nd piece of code, I would be happy too... Thanks for any help.

推荐答案

我还在Slim中使用Capsule.首先,请确保您使用的是Illuminate/Capsule的4.1.*版本.当4.2发布时,最大的变化之一是PHP 5.4成为必需的最低要求. 4.1仍可与PHP 5.3一起使用.

I'm also using Capsule with Slim. First off, make sure that you're using version 4.1.* of Illuminate/Capsule. When 4.2 came out, one of the big changes was that PHP 5.4 became the required minimum. 4.1 will still work with PHP 5.3.

现在,如果您这样做了,但仍然遇到问题,这就是我用Slim的IoC容器注册胶囊的方法:

Now, if you've done that and you're still running into problems, this is how I registered capsule with Slim's IoC container:

$app->container->singleton('capsule', function() use ($config) {
    $capsule = new Illuminate\Database\Capsule\Manager;

    $capsule->setFetchMode(PDO::FETCH_OBJ);
    $capsule->addConnection($config['database']);
    $capsule->bootEloquent();

    return $capsule->getConnection();
});

注意对getConnection()的呼叫.这是必要的,因为Capsule全局未注册.

Note the call to getConnection(). This is necessary, as the Capsule global is not getting registered.

另请注意对setFetchMode的调用.在Laravel中使用Fluent时,查询返回stdClass的实例,但是默认行为似乎是返回关联数组.该框架仅在某个时候覆盖了该限制,从而导致Capsule表现出正确而又出乎意料的方式.这样可以使其以更多预期的行为运行.

Also note the call to setFetchMode. When using Fluent from within Laravel, queries return instances of stdClass, but the default behavior appears to be to return associative arrays. The framework just overrides that at some point, leading to Capsule behaving in correct, yet unexpected ways. This sets it to run with more expected behavior.

这篇关于如何使用Illuminate Database Query Builder&在PHP 5.3.x的其他框架中雄辩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆