PHP code example of veasin / ff-ddd
1. Go to this page and download the library: Download veasin/ff-ddd 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/ */
veasin / ff-ddd example snippets
ddd::define('corp/user', [
'source' => ['table' => 'corp_users', 'primary' => 'id', 'db' => 'default'],
'defs' => ['active' => ['status', 1], 'newest' => ['created_at', null, 'desc']],
'uniques' => ['id', 'email'],
'mutators' => ['password' => static fn($v) => password_hash($v, PASSWORD_DEFAULT)],
'order' => 'created_at',
'entity' => UserEnt::class,
// 'base' => 'base/user',
], null);
ddd::define('corp/user', null, [
'source' => ['table' => 'corp_users', 'primary' => 'id', 'db' => 'default'],
'relations' => ['tokens' => ['concept' => 'corp/user/token', 'via' => ['user_id' => 'id']]],
'uniques' => ['id', 'email'],
'mutators' => ['password' => static fn($v) => password_hash($v, PASSWORD_DEFAULT)],
// 'base' => 'base/user',
]);
ddd::define('blog/post', [
'source' => ['table' => 'posts', 'primary' => 'id'],
'with' => [
'list' => [ // list() 默认读取的段
'author' => [ // 别名 = join 表别名
'concept' => 'blog/user',
'via' => ['user_id' => 'id'], // targetField => currentField(ON 条件)
'fields' => ['name', 'avatar'], // 带出字段;缺省 = 全部(别名.*);列表直接输出,key=>value 重命名
],
],
'get' => [...], // get() 默认读取的段
'自定义段' => [...],
],
], null);
ddd::collection('blog/post')->then->list(['page' => 1]); // list 段
ddd::collection('blog/post')->then->list(['page' => 1], 'custom'); // 自定义段
ddd::collection('blog/post')->then->list(['page' => 1], null); // 禁用
ddd::collection('blog/post')->id(5)->then->get(); // get 段
use ff\ddd\ddd;
use ff\ddd\trigger\entity as entityTrigger;
// 1. 自定义 Trigger:继承单体触发基类,写业务方法
class DirsTrigger extends entityTrigger {
public function launch(): array {
$dir = $this->get(); // 复用原生 get()
// ... 执行业务逻辑(文件系统、exec 等)
return $dir;
}
}
// 2. lv0 配置注入 trigger 类名
ddd::define('dirs', [
'source' => ['table' => 'dirs', 'primary' => 'id'],
'trigger' => DirsTrigger::class,
]);
// 3. 使用:链式走到 then,自动返回自定义 Trigger 实例
$dir = ddd::collection('dirs')->id(5)->then->launch();
ddd::define('dirs', [
'source' => ['table' => 'dirs', 'primary' => 'id'],
'entity' => DirsEntity::class, // id()/create()/findUnique() fork 到 DirsEntity
]);
use function ff\{container, db};
use ff\ddd\ddd;
// 1. 注册概念(集合与单体配置分离)
ddd::define('corps', [
'source' => ['table' => 'corps', 'primary' => 'id'],
], [
'relations'=> ['users' => ['concept' => 'corp/users', 'via' => ['id' => 'corp_id']]],
]);
ddd::define('corp/users', [
'source' => ['table' => 'corp_users', 'primary' => 'id'],
'order' => ['created_at' => 'desc'],
'uniques' => ['id', 'email'],
'mutators' => ['password' => static fn($v) => password_hash($v, PASSWORD_DEFAULT)],
'defs' => ['active' => ['status', 1], 'newest' => ['created_at', null, 'desc']],
], [
'relations'=> ['tokens' => ['concept' => 'corp/user/token', 'via' => ['user_id' => 'id']]],
]);
ddd::define('corp/user/token', [
'source' => ['table' => 'corp_user_tokens', 'primary' => 'id'],
]);
// 2. 列表(集合,范围方法 active() 来自配置的 defs)
ddd::collection('corp/users')->active()->then->get();
// 3. 读取单体
ddd::collection('corp/users')->id(5)->then->get();
// 4. 新建(一级 rules:unique/mutators 自动生效)
ddd::collection('corp/users')->create(['email' => '[email protected] ', 'password' => 'secret'])->then->persist();
// 5. 关联创建(依赖链自动处理:先 persist user 得 id,再回填 token.user_id)
ddd::collection('corps')
->id(5)
->users()
->create(['email' => '[email protected] ', 'password' => 'x'])
->tokens()
->create(['token' => 'xxx', 'client' => 'web'])
->then->persist(); // 返回 token 的持久化结果
// 6. 更新 / 删除 / 计数 / 存在性
ddd::collection('corp/users')->id(5)->update(['nick_name' => '新名'])->then->persist();
ddd::collection('corp/users')->id(5)->then->delete();
ddd::collection('corp/users')->active()->then->count();
ddd::collection('corp/users')->findUnique(['email' => '[email protected] '])->then->exists();
// 7. 配置继承(base)
ddd::define('corp/vip/users', [
'base' => 'corp/users',
'defs' => ['vip' => ['is_vip', 1]],
]);
ddd::collection('corp/vip/users')->vip()->then->get();
// 8. 显式定义(类形态)
class VipUsers extends \ff\ddd\collection{
public const array CONCEPT = [
'base' => 'corp/users',
'defs' => ['vip' => ['is_vip', 1]],
];
}
ddd::define('corp/vip2/users', VipUsers::class);
ddd::collection('corp/vip2/users')->vip()->then->get();
// 9. with 查询级联(list 默认段;自定义段名见上文)
ddd::define('blog/post', [
'source' => ['table' => 'posts', 'primary' => 'id'],
'with' => ['list' => ['author' => ['concept' => 'blog/user', 'via' => ['user_id' => 'id'], 'fields' => ['name']]]],
]);
ddd::collection('blog/post')->then->list(['page' => 1]); // JOIN 带出 author.name
bash
# 自动生成 .phpstorm.meta.php + _ddd.php(桩类命名空间 _ddd)
php vendor/bin/ddd.php
# 监控模式:文件变动自动重生成
php vendor/bin/ddd.php --watch