kappa/doctrine
Composer 安装命令:
composer require kappa/doctrine
包简介
Collection of classes for better work with Doctrine
README 文档
README
Kappa\Doctrine
Collection of classes for better work with Doctrine
Requirements
- PHP 5.4 or higher
- Doctrine 2
- Nette Framework
- Kdyby\Doctrine
Installation:
The best way to install Kappa\Doctrine is using Composer
$ composer require kappa/doctrine:@dev
Usages
Converter::entityToArray()
Method entityToArray requires entity object and returns Kappa\Doctrine\Converters\EntityToArrayConverter.
setIgnoreList(array)- set list of items which you can ignore (ignore list and white list can be combined)setWhiteList(array)- set list of items which you can transform (ignore list and white list can be combined)addFieldResolver(column name, resolver)- you can set closure or concrete value for fieldconvert()- returns generated array
Example:
<?php $user = new User("Joe"); $user->setParent(new User("Joe senior")) ->setAge(50); ->setPrivate("private"); $array = $converter->entityToArray($user) ->setIgnoreList(["private"]) ->addFieldResolver("age", 10) ->addFieldResolver("parent", function(User $parent) { return $parent->getName(); }) ->convert(); echo $array['name']; // print Joe echo $array['parent']; // print Joe senior echo $array['age']; // print 10
Converter::arrayToEntity()
Method arrayToEntity requires two argument. First argument can be entity object or entity class name and returns
Kappa\Doctrine\Converters\ArrayToEntityConverter.
setIgnoreList(array)- set list of items which you can ignore (ignore list and white list can be combined)setWhiteList(array)- set list of items which you can transform (ignore list and white list can be combined)addItemResolver(column name, resolver)- you can set closure or concrete value for itemconvert()- returns generated array
Example:
$data = [ 'name' => 'Joe', 'age' => 50, 'parent' => 1, 'sex' => 'male', 'private' => 'text', ]; $entity = $converter->arrayToEntity('User', $data) ->setIgnoreList(['private']) ->setWhiteList(['age', 'name', 'private']) ->setItemResolver('parent', function ($parent) { return $this->dao->find($parent); }) ->setItemResolver('sex', 'female') ->convert(); echo $entity->getName(); // print Joe echo $entity->getSex(); // print female $entity->getParent(); // returns instance of User
CrudManager
Recommended way for create instance of Kappa\Doctrine\Managers\CrudManager
is use Kappa\Managers\CrudManagerFactory.
<?php $crudManager = $this->crudManagerFactory->create(new User()); // or $crudManager = $this->crudManagerFactory->create('Some\Entity\User');
Method create() requires only one argument which it can be instance of entity or full namespace name.
Created CrudManager contains three methods for basic works with entity.
create(array)- Create a new entity and fill with dataupdate(id, array)- Find entity byidand fill with datadelete(id)- Delete entity withid
FormItemsCreator
$form = new Form(); $form->addSelect('parent', 'Parent item: ', $this->formItemsCreator->create('\UserEntity', new GetAll()); // or $user = new User(); $form->addSelect('parent', 'Parent item: ', $this->formItemsCreator->create($user, new GetAll());
$this->formItemsCreator->create('\UserEntity', new GetAll());
use default columns id and title and create array like this
$array = [ '1' => 'John' ];
You can change default columns via config
doctrine: forms: items: identifierColumn: id valueColumn: name
or as a third and fourth argument
$this->formItemsCreator->create('\UserEntity', new GetAll(), 'name', 'id');
Third argument is valueColumn and last argument is identifierColumn
QueryExecutor
Time to time is needed run DQL query instead of manipulate with entity. Great way is build UPDATE (or DELETE) with QueryBuilder.
Is very useful to create a query object for such cases. In Doctrine and Kdyby\Doctrine
you can create SELECT query and run with $this->repository->fetch(new QueryObject) but UPDATE or DELETE query is not supported. QueryExecutor
is precisely for these situations.
Example:
<?php class ExecutableQuery implements Executable { /** * @param QueryBuilder $queryBuilder * @return QueryBuilder */ public function build(QueryBuilder $queryBuilder) { $queryBuilder->update('KappaTests\Mocks\FormItemsEntity', 'r') ->set('r.title', $queryBuilder->expr()->literal('UPDATED')) ->where('r.id = ?0') ->setParameters(1); return $queryBuilder; } } // and $this->queryExecutor->execute(new ExecutableQuery()); ## RouteParamsResolver You can use `Kappa\Doctrine\Routes\RouteParamsResolver` for easy works with `FILTER_IN/OUT` in your routes **Example** ```php <?php class Router { private $paramsResolver; public function __construct(RouteParamsResolverFactory $factory) { $this->paramsResolver = $factory->create('App\Entities\Article'); } /** * @return \Nette\Application\IRouter */ public function createRouter() { $router = new RouteList(); $router[] = new Route('<presenter>/<action>[/<id>', [ 'presenter' => 'Homepage', 'action' => 'default', 'id' => [ Route::FILTER_IN => [$this->paramsResolver, 'filterIn'], Route::FILTER_IN => [$this->paramsResolver, 'filterOut'] ] ]); return $router; } }
kappa/doctrine 适用场景与选型建议
kappa/doctrine 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.7k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2013 年 06 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 「database」 「orm」 「doctrine」 「nette」 「Kappa」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kappa/doctrine 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kappa/doctrine 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kappa/doctrine 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
Dibi is Database Abstraction Library for PHP
Kinikit - PHP Application development framework MVC component
Store your language lines in the database, yaml or other sources
PHP Database ORM for Symfony1. Do NOT use for new projects: please move to a newest Symfony release and Doctrine2
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
统计信息
- 总下载量: 1.7k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 10
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2013-06-08