walnut / lib_modelmapper_orm
此包的最新版本(dev-master)没有可用的许可证信息。
dev-master
2022-05-13 21:29 UTC
Requires
- php: >=8.1.0
- walnut/lib_dborm: >=0.0.3
- walnut/lib_identitygenerator: >=0.0.1
- walnut/lib_modelmapper: >=0.0.5
Requires (Dev)
- phpunit/phpunit: ^9.5.20
- vimeo/psalm: ^4.23.0
- walnut/lib_dbquery_pdo: ^0.0.3
This package is not auto-updated.
Last update: 2024-09-30 05:32:29 UTC
README
一个将关系存储用作模型映射器的适配器
示例
这是一个非常简单的数据模型 - 客户端。
class Client { public function __construct( public /*readonly*/ string $id, public /*readonly*/ string $name ) {} }
需要一个序列化/反序列化逻辑。
/** * @implements ModelBuilder<Client> * @implements ModelParser<Client> */ final class ClientSerializer implements ModelBuilder, ModelParser { /** * @param array $source * @return Client */ public function build(array $source): object { return new Client( $source['id'] ?? '', $source['name'] ?? '' ); } /** * @param Client $source * @return array */ public function parse(object $source): array { return [ 'id' => $source->id, 'name' => $source->name ]; } }
所有序列化器可以聚合到一个工厂类中
final class MapperFactory implements ModelBuilderFactory, ModelParserFactory { /** * @param string $className * @return ModelBuilder&ModelParser */ private function getSerializer(string $className): object /*ModelBuilder&ModelParser*/ { return match($className) { Client::class => new ClientSerializer, default => throw new RuntimeException("Unknown class $className") }; } public function getBuilder(string $className): ModelBuilder { return $this->getSerializer($className); } public function getParser(string $className): ModelParser { return $this->getSerializer($className); } }
定义关系模型
#[ModelRoot('clients')] class ClientDbModel { public function __construct( #[Table("clients")] #[KeyField('id'), Fields('name')] public array $clients ) {} }
使用MySql存储
$sqlQuoter = new MysqlQuoter; $queryExecutor = new PdoQueryExecutor($connector); $dataModelFactory = new DataModelFactory($sqlQuoter, $queryExecutor); $serializerFactory = new MapperFactory; //Mapping between the models and their storage keys: $configuration = new OrmModelMapperConfiguration( [Client::class => ClientDbModel::class] ); //Mapper factory covering all models $modelMapperFactory = new OrmModelMapperFactory( $configuration, new DataModelBuilder, $dataModelFactory, $serializerFactory, $serializerFactory, $sqlQuoter ); //Take the mapper for the Client model $mapper = $modelMapperFactory->getMapper(Client::class); //Prepare two records $firstClient = new Client('cl-1', 'Client 1'); $secondClient = new Client('cl-2', 'Client 2'); //Using the storage $mapper->all(); //0 entries $mapper->store($firstClient->id, $firstClient); $mapper->store($secondClient->id, $secondClient); $mapper->all(); //2 entries $updatedSecondClient = new Client('cl-2', 'Client 2 new name'); $mapper->store($updatedSecondClient->id, $updatedSecondClient); $mapper->all(); //2 entries $mapper->byId($firstClient->id)->name; //Client 1 $mapper->byId($updatedSecondClient->id)->name; //Client 2 new name $mapper->remove($firstClient->id); $mapper->byId($firstClient->id); //null $mapper->all(); //1 entry