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文件(控制器)

    load->model("Reviews_model"); $student = Reviews_model::find($id); var_dump($student); } } ?>
  7. 创建Reviews_model.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; } } }

    ?>

你可能感兴趣的:(CI)