软飞/generator-bundle

生成辅助代码:从 Doctrine 到 REST

安装: 10

依赖: 0

建议者: 0

安全: 0

星星: 0

关注者: 2

分支: 0

开放问题: 0

类型:symfony-bundle

dev-master 2016-02-13 23:16 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:36:35 UTC


README

安装

composer require softfly/generator-bundle

功能

  1. Doctrine 到 REST

基于 Doctrine 实体的 FOSRestBundle 上生成 REST

  1. 方法:生成所有实体 * 生成映射实体到响应对象。 * 生成关联实体的递归映射。 * 防止 "循环引用" 的保护

示例

namespace AppBundle\Controller\Rest\Properties;

use FOS\RestBundle\Controller\Annotations\RouteResource;
use FOS\RestBundle\Controller\FOSRestController;

class PropertyRest extends FOSRestController
{
    public function getPropertiesAction()
    {
        $data = array();
        /* @var $propertiesRepo \Doctrine\ORM\EntityRepository */
        $propertiesRepo = $this->getDoctrine()->getRepository('\\AppBundle\\Entity\\Properties\\Property');
        $properties = $propertiesRepo->findAll();
        /* @var $property \AppBundle\Entity\Properties\Property*/
        foreach ($properties as $property) {
            $row = array();
            $row['id'] = $property->getId();
            $row['name'] = $property->getName();
            $row['slug'] = $property->getSlug();
            $row['location'] = $property->getLocation();
            $row['short_description'] = $property->getShortDescription();
            $row['long_description'] = $property->getLongDescription();
            /* @var $price \AppBundle\Entity\Offers\Price*/
            foreach ($property->getPrices() as $price) {
                $row1 = array();
                $row1['id'] = $price->getId();
                $row1['price'] = $price->getPrice();
                $row['price'][] = $row1;
                /* @var $timeUnit \AppBundle\Entity\Offers\TimeUnit*/
                $timeUnit = $price->getTimeUnit();
                if ($timeUnit) {
                    $row2 = array();
                    $row2['id'] = $timeUnit->getId();
                    $row2['noun'] = $timeUnit->getNoun();
                    $row2['adverb'] = $timeUnit->getAdverb();
                    $row['timeUnit'] = $row2;
                }
            }
            $data[] = $row;
        }
        $view = $this->view($data, 200);
        return $this->handleView($view);
    }
}