think-orm

webman/think-orm เป็นส่วนประกอบฐานข้อมูลที่พัฒนาขึ้นจาก top-think/think-orm รองรับการใช้เชื่อมต่อแบบ pool และสามารถใช้งานในสภาพแวดล้อมของ coroutine และ non-coroutine

หมายเหตุ
คู่มือนี้สำหรับเวอร์ชัน webman v2 หากคุณใช้เวอร์ชัน webman v1 โปรดดูที่ คู่มือเวอร์ชัน v1

ติดตั้ง think-orm

composer require -W webman/think-orm

หลังจากติดตั้งแล้ว จำเป็นต้อง restart (การ reload ไม่ทำงาน)

ไฟล์การตั้งค่า

ปรับเปลี่ยนไฟล์การตั้งค่า config/think-orm.php ตามความเหมาะสม

ที่อยู่เอกสาร

https://www.kancloud.cn/manual/think-orm

การใช้งาน

<?php
namespace app\controller;

use support\Request;
use support\think\Db;

class FooController
{
    public function get(Request $request)
    {
        $user = Db::table('user')->where('uid', '>', 1)->find();
        return json($user);
    }
}

การสร้างโมเดล

โมเดล think-orm สืบทอดมาจาก support\think\Model มีลักษณะดังนี้

<?php
namespace app\model;

use support\think\Model;

class User extends Model
{
    /**
     * ตารางที่เกี่ยวข้องกับโมเดล
     *
     * @var string
     */
    protected $table = 'user';

    /**
     * คีย์หลักที่เกี่ยวข้องกับตาราง
     *
     * @var string
     */
    protected $pk = 'id';

}

คุณยังสามารถใช้คำสั่งต่อไปนี้เพื่อสร้างโมเดลที่ใช้ think-orm

php webman make:model ชื่อโต๊ะ

提示
คำสั่งนี้ต้องติดตั้ง webman/console โดยคำสั่งติดตั้งคือ composer require webman/console ^1.2.13

หมายเหตุ
หากคำสั่ง make:model ตรวจพบว่าโปรเจกต์หลักใช้ illuminate/database จะสร้างไฟล์โมเดลที่ใช้ illuminate/database แทนที่จะเป็นของ think-orm ในกรณีนี้สามารถใช้พารามิเตอร์เพิ่มเติม tp เพื่อบังคับให้สร้างโมเดลของ think-orm โดยคำสั่งจะคล้ายกับ php webman make:model ชื่อโต๊ะ tp (หากไม่ทำงาน โปรดอัปเกรด webman/console)