Созданный мной контроллер знает слишком много об источнике данных, но приложение спроектировано таким образом, чтобы не ожидать этого.
Данные источником может быть DB, JSON или XML.
Есть ли какой-нибудь способ реализовать для этого интерфейсы?
Мой контроллер знает расположение XML-файла и просматривает разные данные отдельно. Я хочу сделать это одним действием.
Это мой текущий контроллер;
Код: Выделить всё
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');
}
Код: Выделить всё
namespace Tyre\TyreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Products
*/
class Products
{
//some getter and setters and private attribute
}
Я зарегистрировал службу следующим образом (я думаю, что делаю неправильно) в /src/Tyre/TyreBundle/Resources/config/services.yml:
Код: Выделить всё
services:
Tyre\TyreBundle\Repository\DoctrineProductRepository:
class: Tyre\TyreBundle\Repository\DoctrineProductRepository
Tyre\TyreBundle\Repository\ProductRepositoryInterface:
class: Tyre\TyreBundle\Repository\ProductRepositoryInterface
ContextErrorException: Примечание: Неопределенный индекс: в
/home/smiles/Documents/tyre/src/Tyre/TyreBundle/Controller/DefaultController.php
строке 56
Строка 56: это строка: $serviceName = $repositoryMap[$request->get('db')];
Подробнее здесь: https://stackoverflow.com/questions/490 ... ata-source
Мобильная версия