How to get appliaction config in the entity class

I am migrating fro ZF2 to Laminas. I used ServiceLocatorAwareInterface to get config into my entity classes:

<?php
namespace Application\Model;

use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
 
class ServiceLocatorAwareEntity implements ServiceLocatorAwareInterface
{
    protected $sm;
    protected $config;
	
    /**
     * Set the service locator
     *
     * @param ServiceLocatorInterface $sm
     *
     * @return void
     */
    public function setServiceLocator(ServiceLocatorInterface $sm)
    {
        $this->sm = $sm;
		$this->config = $sm->get('Config');
    }
 
    /**
     * Get the service locator
     *
     * @return ServiceLocator ServiceLocator instance
     */
    public function getServiceLocator()
    {
        return $this->sm;
    }
}

and the entity:

<?php
namespace Application\Entity;

use Application\Model\ServiceLocatorAwareEntity;

class Photo extends ServiceLocatorAwareEntity
{
	public function getData()
	{
		return $this->config['photo'];  
	}
}

I am struggling with this in Laminas. How can it be done? This Photo entity is used as doctrine entity so i think it can’t have parametrized constructor.

Hello bartek_78,

I’ve never used AwareEntity … but I would create PhotoFactory class and pass config in constructor. Just an idea.

Hi bartek_78.

Here’s an untested possible solution. Take a look at:

vendor/laminas/laminas-servicemanager/src/Factory/FactoryInterface.php

Then make a factory like:

<?php
//src/Factory/Entity/PhotoEntityFactory.php
namespace Application\Factory\Entity;

use Application\Entity\PhotoEntity;
use Interop\Container\ContainerInterface;
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
use Laminas\ServiceManager\Factory\FactoryInterface;

class PhotoEntityFactory implements FactoryInterface
{
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        $entity = new PhotoEntity();

        $config = $container->get('config');
        if (!isset($config['photo']) || !is_array($config['photo'])) {//Check relevant config exists
            throw new ServiceNotCreatedException("Missing client config: ['photo'].");
        }
        $entity->setConfig($config);

        return $entity;
    }
}

, and then an Entity class like:

<?php
//src/Entity/PhotoEntity.php
namespace Application\Entity;

class PhotoEntity //maybe extends some abstract entity class, and/or implements some entity interface
{
    protected $config;

    //functions using config
    public function getData()
    {
        return $this->config;
    }

    public function setConfig(array $config): void
    {
        $this->config = $config;
    }
}

, and set the link in config:

<?php
//config/module.config.php
use Application\Entity\PhotoEntity;
use Application\Factory\Entity\PhotoEntityFactory;

...
    'service_manager' => [
        'factories' => [
            PhotoEntity::class => PhotoEntityFactory::class,
        ],
    ],
...

Hello and welcome to our forums! :smiley:

This will not work in this case because @bartek_75 uses Doctrine here.

Oh, thank you froschdesign, I was too fast, I better learn what that means :slight_smile: Please advice if I should delete my suggestion?