openpsa/midgard-portable 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 openpsa/midgard-portable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 22.75k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 12
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 3
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: LGPL-2.1-or-later
  • 更新时间: 2014-04-20