zoukankan      html  css  js  c++  java
  • laravel orm独立使用

    2019年12月26日09:43:14

    github:https://github.com/illuminate/database

    为什么要独立使用这个orm,有以下几个原因

    第一:功能实在太强大,写起来舒服,代码复用好

    第二:性能不错

    目前已经配套到laravel 6.x,目前需要php7.2以上

    随便找个文件夹执行

    composer require illuminate/database

    在引入到你需要使用的文件里面

    官方使用说明:

    用于PHP的完整数据库工具包,提供了表达性查询构建器,ActiveRecord样式ORM和模式构建器。目前,它支持MySQL,Postgres,SQL Server和SQLite。它还充当Laravel PHP框架的数据库层。
    

    使用说明

    首先,创建一个新的“胶囊”管理器实例。Capsule的目的是使配置库尽可能容易地在Laravel框架之外使用。

    use IlluminateDatabaseCapsuleManager as Capsule;
    
    $capsule = new Capsule;
    
    $capsule->addConnection([
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'database',
        'username'  => 'root',
        'password'  => 'password',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ]);
    
    // Set the event dispatcher used by Eloquent models... (optional)
    use IlluminateEventsDispatcher;
    use IlluminateContainerContainer;
    $capsule->setEventDispatcher(new Dispatcher(new Container));
    
    // Make this Capsule instance available globally via static methods... (optional)
    $capsule->setAsGlobal();
    
    // Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
    $capsule->bootEloquent();
    composer require "illuminate/events" required when you need to use observers with Eloquent.

    一旦注册了Capsule实例。您可以这样使用它:

    
    

    使用查询生成器

    $users = Capsule::table('users')->where('votes', '>', 100)->get();
    可以从Capsule直接访问其他核心方法,方法与从DB Facade相同:
    
    $results = Capsule::select('select * from users where id = ?', [1]);
    使用架构生成器
    
    Capsule::schema()->create('users', function ($table) {
        $table->increments('id');
        $table->string('email')->unique();
        $table->timestamps();
    });
    使用 Eloquent ORM
    
    class User extends IlluminateDatabaseEloquentModel {}
    
    $users = User::where('votes', '>', 1)->get();
    

    demo

    include __DIR__ . '/vendor/autoload.php';
    include_once __DIR__ . '/Model/User.php';
    
    $database = [
        'driver' => 'mysql',
        'host' => 'localhost',
        'database' => 'test',
        'username' => 'root',
        'password' => 'root',
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix' => '',
    ];
    
    use IlluminateContainerContainer;
    use IlluminateDatabaseCapsuleManager as Capsule; 
    use appmodelsUser;
    
    $capsule = new Capsule;
    // 创建链接
    $capsule->addConnection($database);
    // 设置全局静态可访问
    $capsule->setAsGlobal();
    // 启动Eloquent
    $capsule->bootEloquent();
    
    //$user = Capsule::table('user')->get();
    $user = User::get()->toArray();
    print_r($user);

    注意官方的demo不能直接运行的,需要修改一下

    如果在实际项目中运行在加入一个自动加载的class,就基本足够了

  • 相关阅读:
    [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串
    [LeetCode] Number of Segments in a String 字符串中的分段数量
    [LintCode] Longest Common Prefix 最长共同前缀
    [LintCode] Product of Array Except Self 除本身之外的数组之积
    [LintCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间
    [LintCode] Sort List 链表排序
    [LintCode] Find Peak Element 求数组的峰值
    [LintCode] Container With Most Water 装最多水的容器
    [LintCode] Linked List Cycle 单链表中的环
    [LeetCode] 465. Optimal Account Balancing 最优账户平衡
  • 原文地址:https://www.cnblogs.com/zx-admin/p/12100463.html
Copyright © 2011-2022 走看看