Francesco Scala
Francesco Scala

Reputation: 67

Use a service from another in PHP Symfony

I have this service:

<?php
namespace AppBundle\Services\facade;

use AppBundle\Support\Constants;


class ImageFacade extends AbstractFacade {


    public function __construct(\Doctrine\ORM\EntityManager $em) {
        parent::__construct($em);
    }

    public function delete( $id ) {
        return parent::deleteLine($id, (Constants::IMAGE_CLASS_NAME));
    }

    public function getDescription( $link ) {
        $queryResult = $this->entityManager->getRepository(Constants::IMAGE_CLASS_NAME)->findBy( array('link' => $link ));
        if ( count($queryResult) > 0 ) {
            $image = $queryResult[0];
            return $image->getDescription();
        }
        else {
            return null;
        }
    }

    }

and I want use it from another service:

class ImageBn {



public function __construct() {
    //get a reference to ImageFacade
}


}

How can I do that?

thanks

Upvotes: 1

Views: 41

Answers (1)

radonthetyrant
radonthetyrant

Reputation: 1405

You add the ImageFacade as a dependency to the constructor of ImageBtn and register it in your service config file:

class ImageBn {

  private $facade;

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

}

And in service.yml:

service.image_facade:
    class: ...\ImageFacade
    arguments: [@doctrine.orm.entity_manager]

service.image_button:
    class: ...\ImageBn
    arguments: [@service.image_facade]

Upvotes: 3

Related Questions