示例#1
0
 /**
  * @param \Extcode\Cart\Domain\Model\Dto\Product\ProductDemand $demand
  *
  * @return QueryResultInterface|array
  */
 public function findDemanded(\Extcode\Cart\Domain\Model\Dto\Product\ProductDemand $demand)
 {
     $query = $this->createQuery();
     $constraints = [];
     if ($demand->getSku()) {
         $constraints[] = $query->equals('sku', $demand->getSku());
     }
     if ($demand->getTitle()) {
         $constraints[] = $query->like('title', '%' . $demand->getTitle() . '%');
     }
     if (!empty($demand->getCategories())) {
         $categoryConstraints = [];
         foreach ($demand->getCategories() as $category) {
             $categoryConstraints[] = $query->contains('categories', $category);
         }
         $constraints = $query->logicalOr($categoryConstraints);
     }
     if (!empty($constraints)) {
         $query->matching($query->logicalAnd($constraints));
     }
     if ($orderings = $this->createOrderingsFromDemand($demand)) {
         $query->setOrderings($orderings);
     }
     $products = $query->execute();
     return $products;
 }