首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用接口浏览多个数据源的PHP

使用接口浏览多个数据源的PHP
EN

Stack Overflow用户
提问于 2018-03-03 13:22:44
回答 1查看 524关注 0票数 2

我正在创建一个浏览不同数据源的Symphony应用程序。我创建的控制器对数据源了解得太多了,但是应用程序的设计方式并不是那么想的。数据源可以是DB、JSON或XML。有任何方法来实现接口来做到这一点吗?我的控制器知道XML文件的位置,并分别浏览不同的数据。我想做一个动作。那是我现在的控制器;

代码语言:javascript
运行
复制
public function searchAction(Request $request) {

     if ($request->getMethod() == 'POST') {
         $search_for = $request->get('search');
          //getting the searched products from the database
          $repository = $this->getDoctrine()->getRepository('TyreTyreBundle:Products');
          $query = $repository->createQueryBuilder('u')
            ->where("u.name LIKE '%".$search_for."%' or u.manufacturer LIKE '%".$search_for."%'")
            ->getQuery();
          $results = $query->getResult();

          //adding the XML file products
          $file_url = "bundles/tyretyre/xml/products.xml";
          //Convert the products.XML file into a SimpleXMLElement object
          $simpleXMLElementObject = simplexml_load_file($file_url);

          $i=0;
          //the array where will saved the searched products from the XML file
          $xml_result = [];
          //looping the xml object to find matching results
          while ($simpleXMLElementObject->product[$i]) {
              //first we will convert to lower case both searched item and the tested name
              if (strstr(strtolower($simpleXMLElementObject->product[$i]->name),strtolower($search_for))){
              //push that element into the array to display it later in the twig file
              array_push($xml_result, $simpleXMLElementObject->product[$i]);
              }
              $i++;
          }

          //end of products searching from the XML source

           //display the detail page with passing the DB result and XML result arrays
            return $this->render('TyreTyreBundle:Default:detail.html.twig', array('results' => $results,'xml_result' => $xml_result));
     }
     return $this->render('TyreTyreBundle:Default:search.html.twig');
}

我的产品实体:

代码语言:javascript
运行
复制
namespace Tyre\TyreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

/**
 * Products
 */
class Products
{
//some getter and setters and private attribute
}

编辑遵循VolCh解决方案,

我在/src/Tyre/TyreBundle/Resources/config/services.yml:中注册了以下服务(我认为我做错了)

代码语言:javascript
运行
复制
services:
  Tyre\TyreBundle\Repository\DoctrineProductRepository:
     class: Tyre\TyreBundle\Repository\DoctrineProductRepository

  Tyre\TyreBundle\Repository\ProductRepositoryInterface:
     class: Tyre\TyreBundle\Repository\ProductRepositoryInterface

但是我得到了以下信息

注意:未定义索引:在/home/smiles/Documents/tyre/src/Tyre/TyreBundle/Controller/DefaultController.php第56行中

第56行:这一行:$serviceName = $repositoryMap[$request->get('db')];吗?

EN

回答 1

Stack Overflow用户

发布于 2018-03-03 13:59:31

你可以:

  • 使用方法\TyreTyreBundle\ProductRepository (或某些DTO)声明ProductDataSource接口(如果您愿意的话)
  • 通过构造函数注入\Doctrine\EntityRepository,xml,json,在DoctrinreProductRepository、XmlProductRepository、JsonProductRepository中实现它
  • 在操作中从容器中获得正确的存储库
  • (可选)创建ProductRepositoryFactory whith (‘db\xml\json’)方法,并将类型传递给控制器,作为‘/datasource/{db xml\x\json}’路由或请求参数(如数据源? type =db‘)的一部分,并在一个常见操作中创建正确的存储库。

添加:

示例(概念的证明,不要使用,php7+):

src/TyreBundle/Repository/ProductRepositoryInterface.php

代码语言:javascript
运行
复制
namespace Tyre\TyreBundle\Repository;

interface ProductRepositoryInterface
{
  function search(string $needle): array;
}

src/TyreBundle/Repository/DoctrineProductRepository.php

代码语言:javascript
运行
复制
namespace Tyre\TyreBundle\Repository;

class DoctrineProductRepository implements ProductRepositoryInterface
{
  public function __constructor(EntityManager $em)
  {
    $this->em = $em;
  }

  public function search(string $needle): array
  {
      $repository = $this->em->getRepository('TyreTyreBundle:Products');
      $query = $repository->createQueryBuilder('u')
        ->where("u.name LIKE '%".$needle."%' or u.manufacturer LIKE '%".$needle."%'")
        ->getQuery();
      return $query->getArrayResult();
  }

}

src/TyreBundle/Repository/XmlProductRepository.php src/TyreBundle/Repository/JsonProductRepository.php

控制器

代码语言:javascript
运行
复制
public function searchAction(Request $request)
{
   $repositoryMap = [
     'db' => DoctrineProductRepository::class,
     'xml' => XmlProductRepository::class,
     'json' => JsonProductRepository::class,
   ];
   $serviceName = $repositoryMap[$request->get('type')];
   /** @var ProductRepositoryInterface */
   $repository = $this->get($serviceName);
   $results = $repository->search($request->get('serxh_for'));
   return $this->render('TyreTyreBundle:Default:detail.html.twig', array('results' => $results));
}

此外,您还应该将Repository类注册为带有名称的服务。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49084604

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档