1. Go to this page and download the library: Download inhere/simple-active-record 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/ */
inhere / simple-active-record 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);