rroek / entity-manager-bundle
一个小型包,为你提供创建 Symfony 实体管理器的抽象基类
Requires
- php: >=5.6
- doctrine/doctrine-bundle: ^1.6
- doctrine/orm: ^2.5
- symfony/validator: ^3.4
This package is auto-updated.
Last update: 2024-09-19 02:36:25 UTC
README
功能
- 为你提供简单接口和抽象,以构建自己的 Symfony 实体管理者
目录
[TOCM]
[TOC]
简介
这个小包为你提供了构建适用于 Symfony 应用程序的通用实体管理器的功能(2.7 ~ .3.4)。目标是使所有实体都像 Sensio Symfony 最佳实践(Sensio Symfony Best Practices)所推荐的那样。
优点
为实体创建一个管理者可以简化你的控制器。你实体的所有范围(创建、读取、更新、删除)都将由实体管理者管理。
这对于大型项目或小型/中型项目都很有用。每次创建都会调用管理者,就像每次选择、更新或删除一样。
目标
团队经常想要创建实体管理者,但可能会遇到一些技术难题。这个包为你提供了一个抽象,使所有实体管理者都采用相同的格式,并已包含 CRUD 方法。
使用
激活包
要使用它,只需执行 composer require rroek/entity-manager-bundle
并在 AppKernel.php 中启用它:
/** * @return array */ public function registerBundles() { $bundles = [ [...] new Rroek\EntityManagerBundle\RroekEntityManagerBundle(), [...]
创建你的实体管理者
在这个例子中,我们将以 "MyPersonalEntity" 类为例,它是我们在 Symfony 项目中的一个 Doctrine 实体。其类仓库是 "MyPersonalEntityRepository"。哇,你没有预料到。对吧?
我们的实体将有一个 ID、一个标签、与其他实体的关系以及对其的获取/设置器。
所以让我们看看有趣的东西
我想有一个管理者,可以分发并处理我实体的所有工作。所有的 CRUD。那么,让我们创建自己的管理者类
在
MyBundle
Entity
MyPersonalEntity.php
...
Manager
MyPersonalEntityManager.php
Repository
MyPersonalEntityRepository.php
...
<?php namespace Acme\MyBundle\Manager; use Rroek\EntityManagerBundle\Model\Manager\AbstractBaseEntityManager; use Rroek\EntityManagerBundle\Model\Manager\EntityManagerInterface as PersonalEntityManagerInterface; use Doctrine\ORM\EntityManagerInterface; use Acme\MyBundle\Entity\MyPersonalEntity; /** * Class MyPersonalEntityManager. */ class MyPersonalEntityManager extends AbstractBaseEntityManager implements PersonalEntityManagerInterface { /** * MyPersonalEntityManager constructor. * * @param EntityManagerInterface $entityManager */ public function __construct(EntityManagerInterface $entityManager) { parent::__construct($entityManager); $this->setEntityClass(MyPersonalEntity::class);//Use your Entity ClassName $this->setEntityClassNamespace('Acme\MyBundle\Entity\MyPersonalEntity');//Use Namespace of your Entity } /** * Bind data array to the given entity. * * @param MyPersonalEntityManager $entity * @param array $data * * @return MyPersonalEntityManager */ protected function bind(MyPersonalEntity $entity, array $data) { /*this function get an existing instance of our Entity, or a new instance (see create & update method on abstraction) All the data to set/update are stocked on $data array. */ $entity->setLabel($this->getValue($data, 'label'));//We call the entity property setter and give "label" key of $data as value $entity->setLinkToAnotherEntity($this->getValue($data, 'anotherEntity', null));//Here the same but for a joined Entity like ManyToOne or OneToOne (if you set no data for key 'anotherEntity' '' will be placed instead so for a join precise null to default value) return $entity; } }
声明你的管理者作为 Symfony 服务,以便于使用
services: [...] # ------ ------ ------ ------ ------ # ENTITY MANAGERS SERVICES # ------ ------ ------ ------ ------ acme_my_bundle.my_personal_entity.entity.manager: class: 'Acme\MyBundle\Manager\MyPersonalEntityManager' arguments: - '@doctrine.orm.entity_manager' calls: - [setValidatorService, ['@validator']]
那么!你的实体管理者已经创建,它已经包含了 CRUD 方法
[...] /** * Returns entity-item with given id. * * @param int $id * * @return object */ public function read($id){...} /** * Returns all entity-items. * * @return object[] */ public function readAll(){...} /** * Creates a new item and set the data which are passed. * * @param array $data * @param bool $persist * @param bool $flush * * @return object */ public function create(array $data, $persist = true, $flush = false){...} /** * Update the entity-item with given id. * * @param int $id * @param array $data * @param bool $flush * @param bool $validate * * @return object */ public function update($id, array $data, $flush = false, $validate = false){...} /** * Delete the entity-item with given id. * * @param int $id * @param bool $flush * * @return null * * @throws EntityNotFoundException */ public function delete($id, $flush = false){...}
以及一些方便的方法
[...] /** * Return dynamically the entity repository whithout needing to specify class. * * @return ObjectRepository Related repository */ public function getRepository(){...} /** * @param object $entity * @param bool $flush */ public function persist($entity, $flush = false){...} /** * Flush. */ public function flush(){...} /** * @return \Doctrine\ORM\Mapping\ClassMetadata */ public function getClassMetadata(){...} /** * @return array */ public function getFieldNames(){...} /** * @return array */ public function getAssociationNames(){...}
注意
请注意,管理者只管理一个实体(实体及其仓库)。
奖金
你将找到两个用于实体创建的通用 Traits。IdTrait 和 LabelTrait。要使用它们
[...] use Rroek\EntityManagerBundle\Model\Entity\IdTrait; use Rroek\EntityManagerBundle\Model\Entity\LabelTrait; [...]
它允许使用相同的 id 或标签声明。内容
/** * @var int * * @ORM\Column(name="id", type="integer", precision=0, scale=0, nullable=false, unique=true) * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") * JMS\Expose */ private $id; /** * Get id. * * @return int */ public function getId() { return $this->id; } /** * Set id. * * @param int $id * * @return object */ public function setId($id) { $this->id = $id; return $this; }
和
/** * @var string * * @ORM\Column(name="label", type="string", length=255) */ private $label; /** * Set label. * * @param string $label * * @return mixed */ public function setLabel($label) { $this->label = $label; return $this; } /** * Get label. * * @return string */ public function getLabel() { return $this->label; }