Laravel雄辩于Codeigniter的整合 [英] Laravel Eloquent integration in Codeigniter
问题描述
以下是我的composer.json文件
require:{
pre>
php:> = 5.5.0,
illuminate / database:5.0
}
autoload:{
classmap:[
application / models
]
}
在index.php文件中,我添加了
require 'vendor / autoload.php';
我有创建
Eloquent.php
应用程序/模型启动雄辩<?php if(!defined('BASEPATH'))exit允许访问);
$ CI =& get_instance();
$ config = $ CI-> db;
$ pdo = new PDO('mysql:host ='。$ config-> hostname。'; dbname ='。$ config-> database,$ config-> username,$ config - >密码);
$ drivers = array(
'mysql'=>'\Illuminate\Database\MySqlConnection',
'pgsql'=>'\Illuminate\\ \\ Database\PostgresConnection',
'sqlite'=>'\Illuminate\Database\SQLiteConnection',
);
$ conn = new $ drivers ['mysql']($ pdo,$ config-> database,$ config-> dbprefix);
$ resolver = new Illuminate\Database\ConnectionResolver;
$ resolver-> addConnection('default',$ conn);
$ resolver-> setDefaultConnection('default');
\Illuminate\Database\Eloquent\Model :: setConnectionResolver($ resolver);
以下是我的
Foo.php
/ p>
<?php if(!define('BASEPATH'))exit('不允许直接脚本访问);
require_once(APPPATH.'models / Eloquent.php');
使用Illuminate \Database\Eloquent\Model作为口才;
class Foo extends Eloquent
{
public $ table =foos;
}
在
Foos.php
控制器我已经尝试过类似下面的<?php if(!defined('BASEPATH'))exit允许直接脚本访问);
class Foos extends CI_Controller
{
public function __construct()
{
parent :: __ construct();
}
public function index()
{
var_dump(Foo :: all());
}
}
但它返回以下错误。
严重性:注意
消息:未定义属性:Foos :: $ db
文件名:models / Eloquent.php
行号:5
做错了?
解决方案我相信你缺少的是另一个类Capsule。检查您的供应商文件夹。它位于
/.../ vendor / illuminate / database / Illuminate / Database / Capsule / Manager.php
您可以使用
require'vendor / autoload.php';
使用Illuminate \Database\Capsule\Manager作为Capsule;
$ CONFIG ['db'] = array(
'driver'=>'mysql',
'host'=>'localhost',
'database' >'testDB',
'username'=>'root',
'password'=>'root',
'charset'=>'utf8' $ b'collation'=>'utf8_unicode_ci',
'prefix'=>''
);
$ capsule = new Capsule;
$ capsule-> addConnection($ CONFIG ['db']);
//通过静态方法使这个Capsule实例在全局可用...(可选)
$ capsule-> setAsGlobal();
//设置Eloquent ORM ...(可选;除非您已使用setEventDispatcher())
$ capsule-> bootEloquent();
一旦你把它作为全局变量,你可以调用它:
FooModel.php使用Illuminate \Database\Eloquent\Model作为口才;
class Foo extends Eloquent
{
public $ table =foos;
}
并调用:
$ b b使用\path\to\Foo;
Foo :: all();
您缺少bootEloquent()函数调用。
$ b b希望它帮助! :)
I am trying to use eloquent orm inside codeigniter.
Following is my composer.json file
"require": { "php": ">=5.5.0", "illuminate/database": "5.0" }, "autoload": { "classmap": [ "application/models" ] }
In index.php file I have I added
require 'vendor/autoload.php';
I have create
Eloquent.php
file in application/models to initiate eloquent<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); $CI = & get_instance(); $config = $CI->db; $pdo = new PDO('mysql:host='.$config->hostname.';dbname='.$config->database, $config->username, $config->password); $drivers = array( 'mysql' => '\Illuminate\Database\MySqlConnection', 'pgsql' => '\Illuminate\Database\PostgresConnection', 'sqlite' => '\Illuminate\Database\SQLiteConnection', ); $conn = new $drivers['mysql']($pdo, $config->database, $config->dbprefix); $resolver = new Illuminate\Database\ConnectionResolver; $resolver->addConnection('default', $conn); $resolver->setDefaultConnection('default'); \Illuminate\Database\Eloquent\Model::setConnectionResolver($resolver);
following is my
Foo.php
model<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); require_once(APPPATH.'models/Eloquent.php'); use Illuminate\Database\Eloquent\Model as Eloquent; class Foo extends Eloquent { public $table = "foos"; }
In
Foos.php
controller I have tried something like following<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Foos extends CI_Controller { public function __construct() { parent::__construct(); } public function index() { var_dump(Foo::all()); } }
But it is returning following error.
Severity: Notice Message: Undefined property: Foos::$db Filename: models/Eloquent.php Line Number: 5
What I am doing wrong?
解决方案I believe what you are missing is another class Capsule. Check your vendor folder. It is located in
/.../vendor/illuminate/database/Illuminate/Database/Capsule/Manager.php
You can use this as
require 'vendor/autoload.php'; use Illuminate\Database\Capsule\Manager as Capsule; $CONFIG['db'] = array( 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'testDB', 'username' => 'root', 'password' => 'root', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '' ); $capsule = new Capsule; $capsule->addConnection($CONFIG['db']); // Make this Capsule instance available globally via static methods... (optional) $capsule->setAsGlobal(); // Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher()) $capsule->bootEloquent();
Once you have made this as global, you can call it using: FooModel.php
use Illuminate\Database\Eloquent\Model as Eloquent; class Foo extends Eloquent { public $table = "foos"; }
and call it as:
use \path\to\Foo; Foo::all();
You were missing the bootEloquent() function call.
Hope it helps! :)
这篇关于Laravel雄辩于Codeigniter的整合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!