Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
165 views
in Technique[技术] by (71.8m points)

symfony - Symfony2 Service with an unique instance

Please i need some help:

I have the following services:

SERVICE CONFIGURATION IN services.yml

services:

xpad.producto_repository:
     class: XpadProductoBundleEntityProductRepository
     factory_service: doctrine.orm.clientes_entity_manager
     factory_method: getRepository
     arguments:
         - XpadProductoBundleEntityProduct


backend_cliente.producto_filters:
     class: XpadBackendClienteBundleFiltersProductFilters
     calls:
         - [setRepository, ["@xpad.producto_repository="]]
     scope: container

AND THE CLASS FOR backend_cliente.producto_filters IS:

namespace XpadBackendClienteBundleFilters;

use XpadProductoBundleEntityProductRepository;

class ProductFilters
{
    private $_queryBuilder;


    public function getQueryBuilder()
    {
        return $this->_queryBuilder;
    }

    public function setQueryBuilder($queryBuilder)
    {
        $this->_queryBuilder = $queryBuilder;
    }

    public function setRepository(ProductRepository $productRepository = null)
    {
        if($this->_queryBuilder == null)
        {
            $this->_queryBuilder = $productRepository->createQueryBuilder('p');
        }
    }
}

AND I HAVE THE FOLLOWING ACTION IN ONE OF MY CONTROLLERS:

class ProductController extends Controller
{ 
 ............
    public function indexAction(Request $request)
    {
      //SOME CODE

       $service_filter = $this->container->get('backend_cliente.producto_filters');
       $queryBuilder = $service_filter->getQueryBuilder();

      //SOME OTHER CODE
    }

MY PROBLEM IS: ANYTIME THAT THE indexAction is execute I GOT A NEW INSTANCE OF backend_cliente.producto_filters SERVICE AND I DON'T KNOW WHY. I NEED AND UNIQUE INSTANCE AS A SINGLETON BECAUSE A HAVE THE $_queryBuilder ATRIBUTTE AND I NEED TO GET THE VALUE OF IT JUST MODIFY ITS VALUE WHEN IS NEEDED;

PLEASE HELP I DON KNOW WHAT I'M DOING WRONG.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Do you have to use the class as a service?

Why don't you use a Singleton, without using a service? Like this:

namespace XpadBackendClienteBundleFilters;

use XpadProductoBundleEntityProductRepository;

class ProductFilters { 
    private $_queryBuilder;
    private static $reference = null;

    public function getInstance(){
        if (self::$reference === null)
            self::$reference = new ProductFilters();
        return self::$reference;
    }

    private function __construct(){}

    public function getQueryBuilder()
    {
        return $this->_queryBuilder;
    }

    public function setQueryBuilder($queryBuilder)
    {
        $this->_queryBuilder = $queryBuilder;
    }

    public function setRepository(ProductRepository $productRepository = null)
    {
        if($this->_queryBuilder == null)
        {
            $this->_queryBuilder = $productRepository->createQueryBuilder('p');
        }

    }
}

And call it with:

...
$filter = ProductFilters::getInstance();
...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...