动态模型
动态模型引擎是 Doggy 低代码平台的核心,通过 @Ef 注解配合 PHP 8 Attributes 标记业务字段,自动生成数据库迁移、API 和管理界面。
概述
动态模型系统基于两个核心注解构建:
Ef:标记业务字段的分组和元数据属性EfGroup:定义字段分组
注解定义在 src/Annotation/Ef.php 和 src/Annotation/EfGroup.php:
php
// src/Annotation/Ef.php
class Ef
{
public $value = [];
public function __construct(array $data)
{
$this->value['group'] = $data['group'] ?? null;
$this->value['bf'] = $data['isBF'] ?? false;
}
}定义模型
php
namespace App\Entity\Organization;
use App\Annotation\Ef;
use App\Entity\Traits\CommonTrait;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
#[ORM\Table(name: 'org_department')]
class Department
{
use CommonTrait;
#[ORM\Id]
#[ORM\Column(type: 'uuid', unique: true)]
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
#[ORM\CustomIdGenerator(class: 'doctrine.uuid_generator')]
private $id;
#[Ef(displayName: '部门名称', required: true)]
#[ORM\Column(length: 100)]
private string $name;
#[Ef(displayName: '部门编码', searchable: true)]
#[ORM\Column(length: 50, nullable: true)]
private ?string $code = null;
}实体管理
在管理后台 低代码平台 → 实体管理(App\Controller\Admin\Platform\EntityController)中可查看和管理实体:
主要实体目录
| 目录 | 实体 | 说明 |
|---|---|---|
Entity/Organization/ | Department, Company, Corporation, Employee, Position, PositionLevel | 组织架构与员工 |
Entity/Platform/ | Entity, EntityProperty, View, ViewField, DataGrid, Menu, DataSource | 低代码引擎 |
Entity/Security/ | PasswordPolicy, PasswordStrengthRule, PasswordResetMethod, WebauthnCredential | 安全体系 |
Entity/Storage/ | File, StorageConfig, UploadSession | 文件存储 |
Entity/System/ | AuditLog, Task, TaskLog, EmailConfig, EmailTemplate, EmailLog, SystemCalendarEvent | 基础设施 |
CommonTrait
所有实体通过 CommonTrait(src/Entity/CommonTrait.php)组合以下能力:
php
trait CommonTrait
{
use SoftDeleteableEntity; // 软删除(deletedAt 字段)
use TimestampableEntity; // 时间戳(createdAt, updatedAt)
use BlameableEntity; // 操作人(createdBy, updatedBy)
#[ORM\ManyToOne(targetEntity: Corporation::class)]
private $corporation;
#[ORM\ManyToOne(targetEntity: Company::class)]
private $company;
}自动生成
定义模型后,EfInitEntityCommand 等命令自动处理:
- 数据库迁移文件生成
- CRUD 界面渲染
- 数据验证规则
- 权限检查
管理后台
进入 低代码平台 → 实体管理(/admin/platform/entity)可进行可视化操作:
- 实体属性编辑(EntityProperty)
- 属性分组管理(EntityPropertyGroup)
- 一键生成迁移
- 关联关系配置