Reputation: 2315
I want to load a variable dynamically with values from an unmapped database (separate Entity Manager "ps"). I created the variable $categories for example.
namespace AppBundle\Entity;
/**
* ModProduct
*
* @ORM\Table(name="mod_product")
* @ORM\Entity
*/
class ModProduct
{
...
public static $categories = [];
...
}
Now I want to fill this property with values. I thought of the following solutions:
The result should simply be, whenever I need ModProduct::$categories it should have fetched the categories once from the secondary database and populated the array for further usage.
Upvotes: 0
Views: 1915
Reputation: 3495
I would probably create a postLoad event-listener and use it to do whatever you need when the entity is loaded.
Therefore just create a new class
// Event listener
namespace Whatever;
use Doctrine\ORM\Event\LifecycleEventArgs;
class MyEventListener
{
public function postLoad(LifecycleEventArgs $args)
{
$entity = $args->getObject();
// your logic here..
}
}
then declare it as a service with related tag
// services.yaml
Whatever\MyEventListener:
tags:
- { name: doctrine.event_listener, event: postLoad, method: postLoad }
More info in the official documentation.
Upvotes: 2