How can I use dynamic entity names in a controller action, in Zend framework 2 using Doctrine 2?
For eg.
public function indexAction()
{
return new ViewModel(array(
'albums' => $this->getEntityManager()->getRepository('Album\Entity\[dynamic_entity_name]')->findAll()
));
}
Also, in first place, can I even USE multiple entities in a single controller, like my case here?
The root of doing something like this is, I basically have two modes in my application, live and test, where the users can save data in either mode (something like sandbox and live modes of a payment gateway back-end).
I need to have two different tables, one for each mode; say for e.g., payment_test and payment_live tables for the payments that user makes.
So in my controller, based on the current mode (test or live) that user is using, the data should be retrieved from / saved to the respective entities (PAYMENT_LIVE or PAYMENT_TEST).
I believe checking conditions for the current mode at all the places is a bad bad idea, hence I will just set it in some CURRENT_MODE CONSTANT once, and then use it for using the entity name dynamically, something like:
public function indexAction()
{
return new ViewModel(array(
'payments' => $this->getEntityManager()->getRepository('Payment\Entity\Payment_'.CURRENT_MODE.')->findAll()
));
}
which will use Payment_live entity for live mode and Payment_test entity for test mode, based on value of CURRENT_MODE = “live” or “test”.
Any thoughts how should I go about implementing this?
My first thought was: Why having two tables and not just an identifier to query the mode for. A field insire you table
paymentscalledpayment_modus(could be a boolean for live true/false) or something.Other than that, of course you can have multiple repositories in one controller.
Kinda difficult to answer since i don’t really understand where your problem actually lies.