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

Php Symfony正在错误的类中查找现有函数,php,symfony,Php,Symfony,我正在Symfony中开发一个应用程序,用于在我更改其他实体中的产品时,从销售线中的不同数据库更新供应商的价格 我有一个用于更新的订阅服务器和一个与数据库联系的服务,并在我更新或创建新产品时获取价格 认购人: use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\Security\Core\SecurityContext; use Doctrine\Common\EventSubsc

我正在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”的未定义方法 类“AppBundle\Entity\Subscriber\SupplierProductSubscriber”


为什么Symfony在订阅服务器中寻找函数,尽管我使用的是
$this

您不应该将服务作为静态类使用。而不是编写
产品::
您应该将服务注入到侦听器中,并将其作为普通对象使用