定制 radnan/rdn-doctrine 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

radnan/rdn-doctrine

Composer 安装命令:

composer require radnan/rdn-doctrine

包简介

Zend Framework 2 Doctrine ORM bridge

README 文档

README

The RdnDoctrine ZF2 module is a simple bridge to the Doctrine ORM library.

How to install

  1. Use composer to require the radnan/rdn-doctrine package:

    $ composer require radnan/rdn-doctrine:1.*
  2. Activate the module by including it in your application.config.php file:

    <?php
    
    return array(
        'modules' => array(
            'RdnDoctrine',
            // ...
        ),
    );

Dependencies

This module relies on the modules: RdnConsole, RdnDatabase, and RdnFactory.

How to use

Entity managers can be registered with the RdnDoctrine\EntityManagerManager service locator using the rdn_entity_managers configuration option.

<?php

return array(
	'rdn_entity_managers' => array(
		'factories' => array(),
		'invokables' => array(),
	),
);

You can also use the managers key to quickly generate an entity manager using simple configuration options:

<?php

return array(
	'rdn_entity_managers' => array(
		'managers' => array(
			'App' => array(
				/* configuration options */
			),
		),
	),
);

Configuring an entity manager is as simple as that! Here we've configured an entity manager with the name App. Assuming our module name is also App the library will set you up with some sane defaults, all of which you can override.

By default, the manager will expect your entities to live inside the App\Entity namespace (or more generally <MANAGER-NAME>\Entity).

Configuration documentation

Controller plugin

Once an entity manager has been configured, you can access both the entity manager or entity repositories from your controller using the entity() plugin.

Since you can register multiple entity managers with different names, by default the plugin will fetch the entity manager with the same name as the module name:

namespace App\Controller;

use App\Entity;

class User
{
	public function createAction()
	{
		$user = new Entity\User;
		$user->setEmail('pot@example.com');

		$this->entity()->persist($user);
		$this->entity()->flush();
	}
}

In order to access an entity repository we call the same entity($name) plugin, only this time we provide an entity name:

namespace App\Controller;

use App\Entity;

class User
{
	public function editAction()
	{
		$id = $this->params('user-id');

		$user = $this->entity('User')->find($id);

		/**
		 * Alternatively we can be more explicit and request the
		 * User entity within the App module
		 */
		$user = $this->entity('App:User')->find($id);
	}
}

The RdnDoctrine\EntityManager\AliasResolver service is used to resolve aliases when one is not provided. For example, if User is given instead of App:User.

Code completion

If you'd like to have code completion for this plugin, include the following in your AbstractController class:

namespace App\Controller;

use Zend\Mvc\Controller\AbstractActionController;

/**
 * @method \Doctrine\ORM\EntityRepository|\Doctrine\ORM\EntityManager entity(\string $name = null) Get the entity manager or a repository for given entity name.
 */
abstract class AbstractController extends AbstractActionController
{
}

Then, simply extend your controllers off of this abstract controller.

Console commands

The module also comes with a set of console commands to manage the database schema and generate proxies.

You can run vendor/bin/console to run and get help on the console commands in the doctrine: namespace.

Shared entities

Usually you will have one module that will contain all your common entities such as User entities etc. You will also register a single entity manager for your application with the same name as this module.

You will then create separate modules for each section of your site. Each module will depend on the entities provided by the common module in addition to providing its own. But all of the modules will use the single shared entity manager.

Let's say our common module is called App and we have another module called Foo. In this case the configuration for the Foo module would look like this:

<?php

return array(
	'rdn_entity_managers' => array(
		'managers' => array(
			'App' => array(
				'table_prefixes' => array(
					'Foo' => 'foo__',
				),
			),
		),

		'modules' => array(
			'Foo' => 'App',
		),
	),
);

This will add the entities provided by Foo (in the Foo\Entity namespace) to the App entity manager and instruct all plugins to use the App entity manager from within the Foo module.

namespace Foo\Controller;

class Bar
{
	public function editAction()
	{
		// We can now access the Foo entity repositories
		$bar = $this->entity('Bar')->find($id);
		// - OR - more explicitly
		$bar = $this->entity('Foo:Bar')->find($id);

		// We can also access the App entity repositories
		$user = $this->entity('User')->find($id);
		// - OR - more explicitly
		$user = $this->entity('App:User')->find($id);

		// We have access to the shared entity manager
		$this->entity()->flush();
	}
}

radnan/rdn-doctrine 适用场景与选型建议

radnan/rdn-doctrine 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 59 次下载、GitHub Stars 达 0, 最近一次更新时间为 2013 年 12 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「orm」 「zend」 「zf2」 「doctrine」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 radnan/rdn-doctrine 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 radnan/rdn-doctrine 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 59
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-12-28