PHP code example of php-comp / lite-activerecord

1. Go to this page and download the library: Download php-comp/lite-activerecord 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/ */

    

php-comp / lite-activerecord example snippets


use PhpComp\LiteActiveRecord\RecordModel

class User extends RecordModel
{
    // ...
    
    /**
     * 表名称(可选定义)
     * @return string
     */
    public static function tableName(): string
    {
        return 'user';
    }

    /**
     * 表的字段。
     *  - 必须定义,只有这里定义的字段才会被保存
     * @return array
     */
    public function columns(): array
    {
        return [
            // column => [type]

            'id' => ['int'],
            'name' => ['string'],
        ];    
    }
    
    
    /**
     * define attribute field translate list(可选定义)
     * @return array
     */
    public function translates(): array
    {
        return [
            // 'field' => 'translate',
            // e.g. 'name'=>'名称',
    }    
    
    /**
     * 数据验证规则(可选定义)
     * - 保存数据之前会自动验证
     * @return array
     */
    public function rules(): array
    {
        return [
            // ['body', 'string'],
            // ['id, createTime', 'int'],
        }
    }
}

$user = User::load($data);
$user->insert();

if ($model->hasError()) {
    var_dump($model->firstError())
}

var_dump($user->id);

$user = User::findById(12);
$user = User::findOne(['name' => 'inhere']);

// 查找多个
$users = User::findAll([
    ['id', '>', 23], 
    'status' => [1, 2], // status IN (1,2)
    ['name', 'like', "%tom%"],
]);

$user->name = 'new name';
$user->update();

// 模型删除
$affected = $user->delete();

// 通过id删除
$affected = User::deleteByPk(23);
bash
composer 
json
{
    "hp-comp/lite-activerecord": "~1.0"
    }
}
bash
git clone https://github.com/php-comp/lite-activeRecord.git