CI 框架 融合 laravel框架的 orm

  1. 首先在项目根目录下写好composer.json文件
    {
    “require”: {
    “illuminate/database”: “>=5.5”,
    }
    }

  2. 打开黑窗口,composer update

  3. 安装完成后,修改database.php数据库配置文件
    use Illuminate\Database\Capsule\Manager as Capsule;
    $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’],
    ]);

     	// Make this Capsule instance available globally via static methods... (optional)
     	$capsule->setAsGlobal();
     	
     	// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
     	$capsule->bootEloquent();
    
  4. 修改autoload.php文件
    将$autoload[‘libraries’] = array(); 改成 $autoload[‘libraries’] = array(‘database’);

  5. 在根目录下index.php页面底部 添加require_once ‘./vendor/autoload.php’;
    记住 一定要在require_once BASEPATH.‘core/CodeIgniter.php’;之前添加

  6. 创建Reviews.php文件(控制器)

    <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Reviews extends CI_Controller { // eloquent orm写法 public function index($id=1){ $this->load->model("Reviews_model"); $student = Reviews_model::find($id); var_dump($student); } } ?>
  7. 创建Reviews_model.php(模型)

    <?php
     defined('BASEPATH') OR exit('No direct script access allowed');
     use iLLuminate\Database\Eloquent\Model as Eloquent;
     class Reviews_model extends Eloquent {
     	protected $table = 'articles';
     	protected $fillable = ['title'];
     	// ci框架原生写法
     	public function __construct() {
     		$this->load->database();
     	}
    
     	public function get_reviews( $id ){ 
     		if( $id != FALSE ){ 
     			$query = $this->db->get_where('student', array('id'=>$id));
     			// $query = $this->db->query("SELECT * FROM student LIMIT 10");
     			// return $query->result();
     			return $query->row_array(); 
     		} else { 
     			return FALSE; 
     		} 
     	}
     }	
    

    ?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值