src/EventListener/ProviderEventListener.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Pimcore\Event\Model\ElementEventInterface;
  4. use Pimcore\Event\Model\DataObjectEvent;
  5. use Pimcore\Model\DataObject\Review;
  6. use Pimcore\Model\DataObject\Solution;
  7. use Pimcore\Model\DataObject;
  8. use Pimcore\Model\DataObject\Search;
  9. use Pimcore\Model\DataObject\Provider;
  10. use Pimcore\Model\DataObject\ClassDefinition;
  11. use App\Services\SearchService;
  12. use \Pimcore\Config;
  13. class ProviderEventListener
  14. {
  15.     public function postUpdate(DataObjectEvent $e
  16.     {
  17.         $object $e->getObject();
  18.         
  19.         if($object instanceof Solution){
  20.             if($object->getProvider()){
  21.                 $idProvider $object->getProvider()->getId();
  22.                 $solution = new Solution\Listing();
  23.                 $solution->addConditionParam('provider__id = ?',$idProvider);
  24.                 // cập nhật vào Provider
  25.                 $provider Provider::getById($idProvider);
  26.                 $provider->setTotalSolution($solution->count());
  27.                 $provider->save();
  28.             }
  29.         }
  30.         if($object instanceof Review){
  31.             $idSolution $object->getSolution()->getId();
  32.           
  33.             if($object->getPublished()){
  34.                 // lấy solution
  35.                 $detailSolution Solution::getById($idSolution);
  36.                 if($detailSolution->getProvider()){
  37.                     $idProvider $detailSolution->getProvider()->getId();
  38.                     // lấy tất cả solution 
  39.                     $solution = new Solution\Listing();
  40.                     $solution->addConditionParam('provider__id = ?',$idProvider);
  41.                     $total 0;
  42.                     foreach($solution as $value){
  43.                         $review = new Review\Listing();
  44.                         $review->addConditionParam("solution__id = ?",$value->getId());
  45.                         $total+=$review->count();
  46.                     }
  47.                     // cập nhật vào Provider
  48.                     $provider Provider::getById($idProvider);
  49.                     
  50.                     // tính tổng tất cả review thuộc provider
  51.                     $provider->setTotalReview($total);
  52.                     $provider->save();
  53.                 }
  54.                
  55.             }
  56.             
  57.         }
  58.        
  59.     }
  60. }