warpsale / ws-apigility-doctrine
Warpsale Doctrine 与 Apigility 的集成
dev-master
2015-09-14 18:57 UTC
Requires
- php: >=5.4.0
- doctrine/orm: 2.5.1
- xiag/rql-command: v1.0.1
- xiag/rql-parser: v1.0.1
- zendframework/zend-paginator: 2.5.1
This package is not auto-updated.
Last update: 2024-09-28 18:47:41 UTC
README
Warpsale Doctrine 与 Apigility 的集成
安装(使用 composer)
- 安装 Apigility;
- 安装 Doctrine;
- 安装 doctrine/doctrine-orm-module;
- 安装 warpsale/ws-apigility-doctrine.
设置
- 将 "_utils/module/Base" 文件夹复制到 Apigility 的 "/module" 文件夹中;
- 将 "Base" 模块添加到 Apigility 的 "/config/modules.config.php" 文件中;
- 追加到 Apigility 的 "/config/autoload/local.php" 文件中
'doctrine' => array( 'connection' => array( 'orm_default' => array( 'driverClass' => 'Doctrine\\DBAL\\Driver\\PDOPgSql\\Driver', 'params' => array( 'host' => 'hostname', 'port' => '5432', 'user' => 'username', 'password' => 'password', 'dbname' => 'database', ), ), ), 'hydrators' => [ 'initializers' => [ 'Base\V1\Model\BaseHydratorInitializer', ], ],
- 创建一个新的 API(例如 Geo)。
- 创建一个新的 REST 服务(例如 Region)
- 在 Apigility 的 "/module/Geo/src/Geo/V1" 文件夹内创建 "Model" 文件夹,并将您的 Doctrine 实体文件复制进去。Region.php 示例
<?php namespace Geo\V1\Model; use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\ArrayCollection; use Base\V1\Model\BaseReadEntity; /** * @ORM\Entity(readOnly=true) * @ORM\Table(name="region", * uniqueConstraints={@ORM\UniqueConstraint(name="geo__region__unm49__key", columns={"unm49"})}, * indexes={@ORM\Index(name="geo__region__level__idx", columns={"level"})}, * schema="geo" * ) */ class Region extends BaseReadEntity { /** * @ORM\Id @ORM\Column(type="smallint", options={"comment":"custom shorted hierarchical code"}) * @var int */ private $id; /** * @ORM\Column(type="string", length=3, options={"comment":"united nations UN M.49 code"}) * @var string */ private $unm49; /** * @ORM\ManyToOne(targetEntity="Region", inversedBy="children") * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="RESTRICT") * @var int */ private $parent; /** * @ORM\OneToMany(targetEntity="Region", mappedBy="parent") * @ORM\OrderBy({"id"="ASC"}) * @var array */ private $children; /** * @ORM\OneToMany(targetEntity="Country", mappedBy="region") * @ORM\OrderBy({"id"="ASC"}) * @var array */ private $countries; /** * @ORM\Column(type="smallint") * @var int */ private $level; /** * @ORM\Column(type="string", length=32) * @var string */ private $name; /** * @return the $id */ public function getId() { return $this->id; } /** * @return the $unm49 */ public function getUnm49() { return $this->unm49; } /** * @return the $parent */ public function getParent() { return $this->parent; } /** * @return the $children */ public function getChildren() { return $this->children; } /** * @return the $countries */ public function getCountries() { return $this->countries; } /** * @return the $level */ public function getLevel() { return $this->level; } /** * @return the $name */ public function getName() { return $this->name; } public function __construct() { $this->children = new ArrayCollection(); $this->countries = new ArrayCollection(); } }
- 追加到 "/module/Geo/config/module.config.php" 文件
'doctrine' => array( 'driver' => array( 'geo_model' => array( 'class' => 'Doctrine\\ORM\\Mapping\\Driver\\AnnotationDriver', 'cache' => 'array', 'paths' => array( 0 => __DIR__ . '/../src/Geo/V1/Model', ), ), 'orm_default' => array( 'drivers' => array( 'Geo\\V1\\Model' => 'geo_model', ), ), ), ),
- 将 "DoctrineModule\Stdlib\Hydrator\DoctrineObject" 应用到 "Hydrator 服务名称";
- 将 "page"、"query" 和 "sort" 词语添加到 "Collection 查询字符串白名单";
- 将 "Entity 类" 修改为 "Geo\V1\Model\Region",并在服务创建后,删除 Apigility 的 "/module/Geo/src/Geo/V1/Rest/Region/RegionEntity.php" 文件;
- 这是一个只读示例,因此在选择 "HTTP 实体方法" 和 "HTTP 集合方法" 的部分,选择 "GET" 选项;
- 在 "页面大小参数" 中写入 "results";
- 保存;
- 在 Apigility 的 "/module/Geo/src/Geo/V1/Rest/Region" 文件夹内创建 "RegionMapper.php" 文件;
- 编写以下代码
<?php namespace Geo\V1\Rest\Region; use Doctrine\ORM\EntityManager; use Doctrine\ORM\Tools\Pagination\Paginator; use DoctrineORMModule\Paginator\Adapter\DoctrinePaginator; use WSApigilityDoctrine\DoctrineFilter; use WSApigilityDoctrine\DoctrineSort; use WSApigilityDoctrine\ReadMapperInterface; use Geo\V1\Model\Region as RegionEntity; class RegionMapper implements ReadMapperInterface { protected $em; public function __construct(EntityManager $em) { $this->em = $em; } public function fetch($id) { $qb = $this->em->createQueryBuilder(); $qb->select('e') ->from(RegionEntity::class, 'e') ->where('e.id = :id'); $qb->setParameters(array( 'id' => $id )); return $qb->getQuery()->getOneOrNullResult(); } public function fetchAll($params = array()) { $qb = $this->em->createQueryBuilder(); $qb->select('e') ->from(RegionEntity::class, 'e') ->leftJoin('e.parent', 'p') ->add('orderBy', 'e.level ASC, e.id ASC'); // QUERY (RQL) AND SORT DoctrineFilter::filter($qb, $params); DoctrineSort::sort($qb, $params); // PAGINATE RESULTS $paginator = new Paginator($qb, $fetchJoinCollection = true); $adapter = new DoctrinePaginator($paginator); return new RegionCollection($adapter); } }
- 编辑 Apigility 的 "/module/Geo/src/Geo/V1/Rest/Region/RegionResource.php" 文件,并
- 创建 "mapper" 受保护的属性
protected $mapper;
* create a constructor:
public function __construct(RegionMapper $mapper) { $this->mapper = $mapper; }
* change the "GET" methods:
/** * Fetch a resource * * @param mixed $id * @return ApiProblem|mixed */ public function fetch($id) { return $this->mapper->fetch($id); } /** * Fetch all or a subset of resources * * @param array $params * @return ApiProblem|mixed */ public function fetchAll($params = array()) { return $this->mapper->fetchAll($params); }
- 编辑 Apigility 的 "/module/Geo/src/Geo/V1/Rest/Region/RegionResourceFactory.php" 文件
<?php namespace Geo\V1\Rest\Region; use Doctrine\ORM\EntityManager; use Geo\V1\Rest\Region\RegionMapper; use Geo\V1\Rest\Region\RegionResource; class RegionResourceFactory { public function __invoke($services) { $em = $services->get(EntityManager::class); $mapper = new RegionMapper($em); return new RegionResource($mapper); } }