nginx -s reload
server {
charset utf-8;
client_max_body_size 128M;
listen 80;
server_name ci-with-orm.com;
root /Users/clive/Workspace/ci_with_orm;
index index.php index.html;
access_log /data/access.log main;
error_log /data/error.log;
location / {
# Redirect everything that isn't a real file to index.php
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
}
location ~* /\. {
deny all;
}
}
# 新增一条
127.0.0.1 ci-with-orm.com
http://ci-with-orm.com/
默认主页
"illuminate/database": "^5.4"
{
"description": "The CodeIgniter framework",
"name": "codeigniter/framework",
"type": "project",
"homepage": "https://codeigniter.com",
"license": "MIT",
"support": {
"forum": "http://forum.codeigniter.com/",
"wiki": "https://github.com/bcit-ci/CodeIgniter/wiki",
"slack": "https://codeigniterchat.slack.com",
"source": "https://github.com/bcit-ci/CodeIgniter"
},
"require": {
"php": ">=5.3.7",
"illuminate/database": "^5.4"
},
"suggest": {
"paragonie/random_compat": "Provides better randomness in PHP 5.x"
},
"require-dev": {
"mikey179/vfsStream": "1.1.*",
"phpunit/phpunit": "4.* || 5.*"
}
}
composer install
,会生成vendor目录,ORM模块也包含在内application/config/autoload.php
// 注释该行
// $autoload['libraries'] = array();
// 新增该行
$autoload['libraries'] = array('database');
application/config/config.php
// 注释该行
// $config['composer_autoload'] = FALSE;
// 新增该行
$config['composer_autoload'] = realpath(APPPATH . '../vendor/autoload.php');
application/config/database.php
//注释以下内容
//$active_group = 'default';
//$query_builder = TRUE;
//
//$db['default'] = array(
// 'dsn' => '',
// 'hostname' => 'localhost',
// 'username' => '',
// 'password' => '',
// 'database' => '',
// 'dbdriver' => 'mysqli',
// 'dbprefix' => '',
// 'pconnect' => FALSE,
// 'db_debug' => (ENVIRONMENT !== 'production'),
// 'cache_on' => FALSE,
// 'cachedir' => '',
// 'char_set' => 'utf8',
// 'dbcollat' => 'utf8_general_ci',
// 'swap_pre' => '',
// 'encrypt' => FALSE,
// 'compress' => FALSE,
// 'stricton' => FALSE,
// 'failover' => array(),
// 'save_queries' => TRUE
//);
// 新增下面内容
use Illuminate\Database\Capsule\Manager as Capsule;
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => '127.0.0.1',
'username' => 'root',
'password' => 'psw',
'database' => 'test',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
$capsule = new Capsule;
$capsule->addConnection([
'driver' => 'mysql',
'host' => $db['default']['hostname'],
'database' => $db['default']['database'],
'username' => $db['default']['username'],
'password' => $db['default']['password'],
'charset' => $db['default']['char_set'],
'collation' => $db['default']['dbcollat'],
'prefix' => $db['default']['dbprefix'],
]);
$capsule->setAsGlobal();
$capsule->bootEloquent();
application/models/Order.php
<?php
use \Illuminate\Database\Eloquent\Model as Eloquent;
# 确保数据库中有orders表
class Order extends Eloquent{
protected $table = 'orders';
}
application/controllers/Welcome.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
var_dump($this->getOrders());
exit(0);
$this->load->view('welcome_message');
}
public function getOrders() {
$this->load->model("Order");
$orders = Order::all()->toArray();
return $orders;
}
}
http://ci-with-orm.com/
就可以通过ORM查询到orders表中的数据了。
orders数据