openpsa/midgard-portable
Composer 安装命令:
composer require openpsa/midgard-portable
包简介
ActiveRecord ORM built on top of Doctrine 2
README 文档
README
This library provides an ActiveRecord ORM built on top of Doctrine 2 and is modeled after the Midgard API.
In a Nutshell
You can define your entities in XML (usually referred to MgdSchema):
<type name="my_person" table="person"> <property name="id" type="unsigned integer" primaryfield="id"> <description>Local database identifier</description> </property> <property name="firstname" type="string" index="yes"> <description>First name of the person</description> </property> <property name="lastname" type="string" index="yes"> <description>Last name of the person</description> </property> </type>
Running midgard-portable schema will create a corresponding database table and a PHP class (usually referred to as the MgdSchema class). You can use this to read from and write to the DB:
// create a new person $person = new my_person(); $person->firstname = 'Alice'; if ($person->create()) { echo 'Created person #' . $person->id; } // load a new copy of the same person $loaded = new my_person($person->id); $loaded->firstname = 'Bob'; if ($loaded->update()) { echo 'Renamed from ' . $person->firstname . ' to ' . $loaded->firstname; }
midgard-portable automatically adds metadata to the record:
$person = new my_person(); $person->firstname = 'Alice'; $person->create(); sleep(1); $person->lastname = 'Cooper'; $person->update(); echo 'Person was created on ' . $person->metadata->created->format('Y-m-d H:i:s'); echo ' and last updated on ' . $person->metadata->updated->format('Y-m-d H:i:s');
It also supports soft-delete:
$person = new my_person(); $person->firstname = 'Alice'; $person->create(); $person->delete(); try { $loaded = new my_person($person->id); } catch (midgard_error_exception $e) { echo $e->getMessage(); // prints "Object does not exist." } // Revert the deletion my_person::undelete($person->guid); // or remove the entry completely $person->purge();
You can query entries like this:
$qb = new midgard_query_builder('my_person'); $qb->add_constraint('metadata.created', '>', '2012-12-10 10:00:00'); $qb->add_order('firstname'); foreach ($qb->execute() as $result) { echo $result->lastname . "\n"; }
Or, you simply use Doctrine's builtin QueryBuilder.
Then, there's object trees, links, working with files, import/export of data and lots more, but until there is time to document all that, you'll have to read the source to find out (the unit tests might also be a good starting point).
Usage
To include midgard-portable in your application, simply require it in your composer.json. You can bootstrap the adapter like this:
<?php use midgard\portable\driver; use midgard\portable\storage\connection; $db_config = [ 'driver' => 'pdo_sqlite', 'memory' => true ]; $schema_dirs = ['/path/to/my/schemas/']; $var_dir = '/path/to/vardir'; $entity_namespace = ''; $dev_mode = false; $driver = new driver($schema_dirs, $var_dir, $entity_namespace); connection::initialize($driver, $db_config, $dev_mode);
Change the parameters as required. After calling connection::initialize(), you can interact with the database through Midgard API as outlined above.
CLI tools
midgard-portable needs to generate entity classes as well as ClassMetadata and Proxy classes for Doctrine. In development setups, this is done automatically on each request. For production installations, you can run the following CLI command:
./bin/vendor/midgard-portable schema
It works very much like the midgard-schema tool of old, i.e. it will generate midgard_object classes based on MgdSchema XML files, the accompanying mapping data and proxy classes, and also create/update the corresponding database tables. You will need to run this once during initial installation, and then again each time the MgdSchemas change.
You can also use Doctrine's CLI runner and all the functionality it provides if you create a file under the name cli-config.php, with this content:
<?php use midgard\portable\storage\connection; require 'my_settings_file.php'; //This needs to contain the code shown above $entityManager = connection::get_em();
openpsa/midgard-portable 适用场景与选型建议
openpsa/midgard-portable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 22.75k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2014 年 04 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 openpsa/midgard-portable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 openpsa/midgard-portable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 22.75k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 12
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: LGPL-2.1-or-later
- 更新时间: 2014-04-20