modul-is / orm
轻量级混合 ORM/探索器
v2.2.0
2024-09-12 13:00 UTC
Requires
- php: ^8.2
- nette/bootstrap: ^3.0
- nette/database: ^3.0 || ^4.0
- nette/utils: ^4.0
Requires (Dev)
- modul-is/coding-standard: ^4
- nette/caching: ^3.0
- nette/robot-loader: ^4.0
- nette/tester: ^2.5
- phpstan/phpstan: ^1
- tracy/tracy: ^2.7
README
许可证
此存储库是在 MIT 许可下对 YetORM 的全面重写。原始分支 kravcik/core 将不再维护 Nette3。您可以在 此处 找到 YetORM 和 Nette 3 的未完成 PR。
摘要
这是一个简单可扩展的数据库层与 ORM 原则的结合。
有关用法和示例,请参阅 快速入门。
只读
ReadonlyProperty 属性可用于不应写入的属性,例如自增列。
#[\ModulIS\Attribute\ReadonlyProperty]
public int $id;
特殊类型与行为
- array - 以 json 格式存储在数据库中
- bool - 以 int 格式存储在数据库中
- \Nette\Utils\DateTime - 保存和加载
\Nette\Utils\DateTime
自定义类型
您还可以使用自定义类型,只需创建一个扩展 \ModulIS\Datatype\Datatype
并实现其两个静态函数的类即可
input(string $name, $value)
- 保存逻辑(转换为数据库兼容类型)output($value)
- 读取逻辑(转换回原始类型)
class File extends \ModulIS\Datatype\Datatype
{
public static function input(string $name, $value): string
{
if($value instanceof \SplFileInfo)
{
$value = $value->getPathname();
}
else
{
throw new \ModulIS\Exception\InvalidArgumentException("Invalid type for column '{$name}' - Instance of '\SplFileInfo' expected, '" . get_debug_type($value) . "' given.");
}
return $value;
}
public static function output($value): self
{
$value = new self(new \SplFileInfo($value));
return $value;
}
}
然后您可以使用属性与您的类型,就像所有常规类型一样。
public \App\Datatype\File $file;
只需确保数据始终包含在指定的类中,以避免错误。
$entity->file = new \App\Datatype\File(new \SplFileInfo('../app/Datatype/File.php'));
bdump($entity->file); //App\Datatype\File(value: SplFileInfo(path: '../app/Datatype/File.php'));
bdump($entity->file->value->getFilename()); //'File.php'