phprest/phprest-service-orm

Phprest Orm 服务。

dev-master 2015-06-28 18:33 UTC

This package is not auto-updated.

Last update: 2024-09-14 16:56:59 UTC


README

Author Software License

描述

使用这些组件的 Orm 服务

安装

通过 composer 安装。

{
    "require": {
        "phprest/phprest-service-orm": "@stable"
    }
}

提示:你应该浏览 phprest/phprest-service-orm 页面来选择一个稳定的版本使用,避免使用 @stable 元约束。

使用方法

配置

对于配置,你应该检查 Config 类。

示例

<?php
$ormConfig = new \Phprest\Service\Orm\Config(
    [
        'driver'            => 'pdo_mysql',
        'host'              => 'localhost',
        'dbname'            => 'phprest',
        'charset'           => 'utf8',
        'user'              => 'root',
        'password'          => 'root'
    ],
    ['path_to_the_entities']
);

$ormConfig->migration = new \Phprest\Service\Orm\Config\Migration('path_to_the_migrations');
$ormConfig->fixture = new \Phprest\Service\Orm\Config\Fixture('path_to_the_fixtures');

注册

<?php
use Phprest\Service\Orm;
# ...
/** @var \Phprest\Application $app */

$app->registerService(new Orm\Service(), $ormConfig);
# ...

从控制器访问

要从控制器访问你的服务,你应该使用服务的 Getter 特性。

<?php namespace App\Module\Controller;

use Phprest\Service;

class Index extends \Phprest\Util\Controller
{
    use Service\Orm\Getter;

    public function post(Request $request)
    {
        /** @var \Doctrine\ORM\EntityManager $em */
        $em = $this->serviceOrm();
    }
}

命令行界面

如果你想在 composer 安装后使用辅助脚本(vendor/bin/phprest-service-orm),你可以这样做。

你必须为脚本提供一个 orm 配置。你有两种选择

  • 将你的 orm 配置放在一个特定文件中: app/config/orm.php
  • 你必须从适当的文件返回 orm 配置
  • 将配置路径放在 paths.php 文件中
  • 你必须从 paths.php 文件返回一个数组,其中包含.orm配置文件的路径,键名为 service.orm.config