Symfony正在寻找错误类中的现有函数

时间:2017-05-12 11:26:06

标签: php symfony

当我在其他实体中更改产品时,我正在Symfony中开发一个应用程序,用于从销售线中的其他数据库更新价格供应商。

我有更新的订阅者和与数据库联系的服务,并在我更新或创建新产品时获取价格。

订阅者:

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Security\Core\SecurityContext;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\OnFlushEventArgs;
use AppBundle\Entity\Sale;
use AppBundle\Entity\SupplierProduct;
use Argent\ShopBundle\Entity\OrderLine;
use Argent\ShopBundle\Entity\Bonus;
use Argent\ShopBundle\Entity\BonusGamer;
use Argent\ShopBundle\Entity\OrderHistory;
use AppBundle\Service\ProductMethods as Products;

class SupplierProductSubscriber implements EventSubscriber
{
    /**
     *
     * @var \Doctrine\ORM\EntityManager
     */
    protected $em;

    /**
     *
     * @var \Doctrine\ORM\UnitOfWork
     */
    protected $uow;

    /**d
     *
     * @var Symfony\Component\Security\Core\SecurityContext
     */
    protected $context;

    /**
     * @var Symfony\Component\DependencyInjection\ContainerInterface
     */
    protected $container;

    /**
     *
     * @return array
     */
    public function getSubscribedEvents()
    {
        return [
            'onFlush'
        ];
    }


    /**
     *
     * @param OnFlushEventArgs $args
     */
    public function onFlush(OnFlushEventArgs $args)
    {

        $this->em = $args->getEntityManager();
        $this->uow = $this->em->getUnitOfWork();

        $entities = $this->uow->getScheduledEntityUpdates();

        if (!empty($entities)) {

            foreach ($entities as $entity) {

                if ($entity instanceof SupplierProduct) {

                    $this->updateSellPrice($entity);
                }
            }
        }
    }

    /**
     *
     * @param object $entity
     */
    private function saveChanges($entity)
    {
        $this->uow->computeChangeSet($this->em->getClassMetadata(get_class($entity)), $entity);
    }

    /**
     *
     * @param SupplierProduct $supplierProduct
     */
    private function updateSellPrice(SupplierProduct $supplierProduct)
    {

        $changes = $this->uow->getEntityChangeSet($supplierProduct);




        if (isset($changes['products'])) {
            $from = $changes['products'][0];
            $to = $changes['products'][1];


            if ($from != $to) {

                $rateSupplier = Products::getSupplierPrice($to->getPartNumber(), $supplierProduct->getSuppliers());


                $sales = $this->em->createQueryBuilder()
                    ->select('e')
                    ->from('AppBundle:Sale', 'e')
                    ->andWhere('e.product = :productId')
                    ->andWhere('e.supplierprice = 0')
                    ->setParameter('productId' , $supplierProduct->getId())
                    ->getQuery()
                    ->getArrayResult()
                ;

                foreach ($sales as $sale) {
                    $sale->setSupplierPrice($rateSupplier);
                    $this->em->persist($sale);
                    $this->saveChange($sale);
                }
            }
        }
    }
}

服务:

use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Doctrine\ORM\EntityRepository;
use Doctrine\DBAL\Query\ExpressionBuilder as Expr;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use AppBundle\Entity\Product;
use AppBundle\Entity\Sale;

class ProductMethods
{

    private $container;

    public function __construct($container)
    {
        $this->container = $container;
    }

    public function getSupplierPrice($sku, $supplier) 
    {            
        $products = $this->getLogicProducts($supplier);
        $price = (array_key_exists($sku, $products)) ? $products[$sku] : 0;

        return $price;
    }  

    private function getLogicProducts($supplier) 
    {
        $repository = $this->getDoctrine()->getRepository('LogicBundle:Supplier', 'logic');
        $tempSupplier = $repository->findOneByName($supplier->getSku());

        if ($tempSupplier->getRate()) {

            if ($supplier->getRate() === null) {
                $supplier->setRate($tempSupplier->getRate());
            }
            $products = $this->getLoadProducts($supplier);
        } else {
            $products = null;
        }
       return $products;
    }

    private function getLoadProducts($supplier)
    {
        $repository = $this->getDoctrine()->getRepository('LogicBundle:Rate', 'logic');

        $products = $repository->findByRate($supplier->getRate());

        foreach ($products as $product) {
            $priceList[trim($product->getSku())] = $product->getPrice();
        }

        return $priceList;
    }

}

当函数getSupplierPrice调用函数getLogicProducts时出现问题,然后我收到此错误消息:

  

尝试调用名为" getLogicProducts"的未定义方法。的   class" AppBundle \ Entity \ Subscriber \ SupplierProductSubscriber

为什么Symfony正在寻找订阅者中的功能,尽管我正在使用$this

1 个答案:

答案 0 :(得分:3)

您不应将服务用作静态类。您应该将服务注入监听器并将其用作普通对象,而不是编写Products::