Probuje wykonac wyszukiwarke, ktora by wyszukiwala produkty danego producenta, jednak mam problem z napisaniem odpowiedniego zapytania.
W kontrolerze mam nastepujacy kod:
class ProductsController extends Controller { /** * Lists all Products entities. */ public function indexAction() { $em = $this->getDoctrine()->getManager(); $entitiesLength = $em->getRepository('MlBackendBundle:Products')->counter(); $entities = $em->getRepository('MlBackendBundle:Products')->findAll(); $brands = $em->getRepository('MlBackendBundle:Products')->getBrands(); $availability = $em->getRepository('MlBackendBundle:Products')->getAvailability(); 'entitiesLength' => $entitiesLength, 'entities' => $entities, 'brands' => $brands, 'availability' => $availability, )); }
W repozytorium zas:
public function search($searchParam) { $qb = $this->createQueryBuilder('product'); $qb->andWhere('product.sku like :keyword or product.name like :keyword') ->setParameter('keyword', '%'.$keyword.'%'); $qb->andWhere('product.id in (:ids)')->setParameter('ids', $ids); $qb->andWhere('product.sku = :sku')->setParameter('sku', $sku); $qb->andWhere('product.name = :name')->setParameter('name', $name); // ponizszy fragment nie zwraca poprawnych wynikow $qb->andWhere('product.brandId = :brandId')->setParameter('brandId', $brandId); $qb->andWhere('product.priceBrutto >= :priceBrutto')->setParameter('priceBrutto', $priceBrutto); $qb->andWhere('product.isNew = :isNew')->setParameter('isNew', $isNew); $qb->andWhere('product.isPromo = :isPromo')->setParameter('isPromo', $isPromo); $qb->andWhere('product.isPopular = :isPopular')->setParameter('isPopular', $isPopular); $qb->andWhere('product.isActive = :isActive')->setParameter('isActive', $isActive); $sortDir = ($sortDir == 'DESC') ? 'DESC' : 'ASC'; $qb->orderBy('product.' . $sortBy, $sortDir); } return new Paginator($qb->getQuery()); } private function fetch($query) { $stmt = $this->getEntityManager()->getConnection()->prepare($query); $stmt->execute(); return $stmt->fetchAll(); } public function counter() { $qb = $this->createQueryBuilder('product')->select('COUNT(product)'); return $qb->getQuery()->getSingleScalarResult(); } public function getBrands() { return $this->fetch("select distinct id, name from brands"); } public function getAvailability() { return $this->fetch("select distinct name as label from availability"); }
Fragment pliku index.hmtl.twig:
W kodzie HTML widnieje zapis:
W profilerze mam takie zapytanie:
SELECT t0.id AS id_1, t0.sku AS sku_2, t0.name AS name_3, t0.slug AS slug_4, t0.vat_value AS vat_value_5, t0.price_netto AS price_netto_6, t0.price_brutto AS price_brutto_7, t0.old_price_netto AS old_price_netto_8, t0.old_price_brutto AS old_price_brutto_9, t0.is_active AS is_active_10, t0.is_new AS is_new_11, t0.is_promo AS is_promo_12, t0.is_popular AS is_popular_13, t0.created AS created_14, t0.updated AS updated_15, t0.views AS views_16, t0.short_description AS short_description_17, t0.description AS description_18, t0.hide_price AS hide_price_19, t0.meta_title AS meta_title_20, t0.meta_keywords AS meta_keywords_21, t0.meta_description AS meta_description_22, t0.availability_id AS availability_id_23, t0.brand_id AS brand_id_24 FROM products t0
Gdzie robie blad? Prosze o jakas wskazowke.
Poprawilem kod i teraz juz dziala.
Dla zainteresowanych tematem:
Repozytorium
public function search($searchParam) { $qb = $this->createQueryBuilder('product') ->leftJoin('product.brands', 'b') ->addSelect('b'); ->leftJoin('product.availability', 'a') ->addSelect('a'); $qb->andWhere('product.sku like :keyword or product.name like :keyword') ->setParameter('keyword', '%'.$keyword.'%'); $qb->andWhere('product.id in (:ids)')->setParameter('ids', $ids); $qb->andWhere('product.sku = :sku')->setParameter('sku', $sku); $qb->andWhere('product.name = :name')->setParameter('name', $name); $qb->andWhere('b.id = :brands')->setParameter('brands', $brands); }
Kontroler:
public function indexAction() { $em = $this->getDoctrine()->getManager(); $entitiesLength = $em->getRepository('MlBackendBundle:Products')->counter(); $entities = $em->getRepository('MlBackendBundle:Products')->findAll(); $brands = $em->getRepository('MlBackendBundle:Brands')->findAll(); $categories = $em->getRepository('MlBackendBundle:Products')->getCategories(); $availability = $em->getRepository('MlBackendBundle:Availability')->findAll(); 'entitiesLength' => $entitiesLength, 'entities' => $entities, 'brands' => $brands, 'categories' => $categories, 'availability' => $availability, )); }
index.html.twig