1. Go to this page and download the library: Download lin-orm/lite library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
lin-orm / lite example snippets
#[Table(name: 'users', comment: '用户表')]
class User
{
#[PrimaryKey, AutoIncrement, Type(type: 'int', length: 11)]
public $id;
}
/**
* @Table(name: 'users', comment: '用户表')
*/
class User
{
/**
* @PrimaryKey
* @AutoIncrement
* @Type(type: 'int', length: 11)
*/
public $id;
}
namespace app\model;
use Lin\Lite\attr\Table;
use Lin\Lite\attr\PrimaryKey;
use Lin\Lite\attr\AutoIncrement;
use Lin\Lite\attr\Type;
use Lin\Lite\attr\DefaultValue;
use Lin\Lite\attr\Comment;
use Lin\Lite\attr\CreatedAt;
use Lin\Lite\attr\UpdatedAt;
use Lin\Lite\attr\BelongsTo;
use Lin\Lite\attr\HasMany;
#[Table(comment: '配置')]
class Config
{
#[PrimaryKey, AutoIncrement, Type('int', 11), Comment('ID')]
public $id;
#[Type('varchar', 100), DefaultValue(''), Comment('名称')]
public $name;
#[Type('int', 11), Comment('配置类型ID'), BelongsTo(ConfigType::class)]
public $config_type_id;
#[Type('text'), Json, Comment('JSON 值')]
public $vals;
#[CreatedAt, Type('datetime'), Comment('创建时间')]
public $created_at;
#[UpdatedAt, Type('datetime'), Comment('更新时间')]
public $updated_at;
#[HasMany(ConfigValue::class)]
public $values;
}
namespace app\model;
use Lin\Lite\attr\Table;
use Lin\Lite\attr\PrimaryKey;
use Lin\Lite\attr\AutoIncrement;
use Lin\Lite\attr\Type;
use Lin\Lite\attr\DefaultValue;
use Lin\Lite\attr\Comment;
use Lin\Lite\attr\CreatedAt;
use Lin\Lite\attr\UpdatedAt;
use Lin\Lite\attr\BelongsTo;
use Lin\Lite\attr\HasMany;
/**
* @Table(comment: '配置')
*/
class Config
{
/**
* @PrimaryKey
* @AutoIncrement
* @Type(type: 'int', length: 11)
* @Comment(comment: 'ID')
*/
public $id;
/**
* @Type(type: 'varchar', length: 100)
* @DefaultValue(value: '')
* @Comment(comment: '名称')
*/
public $name;
/**
* @Type(type: 'int', length: 11)
* @Comment(comment: '配置类型ID')
* @BelongsTo(model: 'app\model\ConfigType')
*/
public $config_type_id;
/**
* @Type(type: 'text')
* @Json
* @Comment(comment: 'JSON 值')
*/
public $vals;
/**
* @CreatedAt
* @Type(type: 'datetime')
* @Comment(comment: '创建时间')
*/
public $created_at;
/**
* @UpdatedAt
* @Type(type: 'datetime')
* @Comment(comment: '更新时间')
*/
public $updated_at;
/**
* @HasMany(model: 'app\model\ConfigValue')
*/
public $values;
}
use Lin\Lite\DB;
// 基于表名
$db = DB::table('config');
// 基于模型(推荐)
$model = new Config();
$db = DB::model($model);
// 简单条件,参数绑定
->where('id = ?', 1)
->where('status = ?', 1)
->where('id > ?', 10)
// WHERE 多个自动 AND
->where('status = ?', 1)->where('sort > ?', 0)
// IN 条件自动展开
->where('id IN (?)', [1, 2, 3])
// 非空才添加条件
->whereNotEmpty('name = ?', $name)
// LIKE 模糊搜索,非空才添加,自动加 %
->like('name', $keyword)
->like('name LIKE ?', $keyword) // 手动写 LIKE 也支持
interface ConnInterface
{
public function getSymbol(): string; // 名称标识符包裹符,如 `、"
public function query(string $sql, array $args = []); // 查询,有返回结果
public function exec(string $sql, array $args = []); // 执行,无返回结果
public function lastInsertId(): int|string; // 最后插入ID
public function begin(); // 开始事务
public function commit(); // 提交事务
public function rollback(); // 回滚事务
}
use Lin\Lite\DB;
// 实现 ConnInterface 后替换连接
DB::setConn(new MyCustomConn());
class Config
{
public function beforeFind() {} // 查询前
public function afterFind() {} // 查询后
public function beforeSave() {} // 保存前, save方法,也会执行插入和更新钩子,反之不会,即插入和更新不会执行save钩子方法,保存后相同
public function afterSave() {} // 保存后
public function beforeCreate() {} // 插入前
public function afterCreate() {} // 插入后
public function beforeUpdate() {} // 更新前
public function afterUpdate() {} // 更新后
public function beforeDelete() {} // 删除前
public function afterDelete() {} // 删除后
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.