I have some code that I use to render the template + values for the HTML that makes up jQuery Modal Boxes in my code.
//code that pulls together data I need from ORM
//assembles it and passes it to HTML template builder
public function renderModalBoxesHtml($id)
{
$query = $this->getEntityManager()->createQuery('
SELECT s
FROM \Entity\SalesItem s
WHERE id = :id
');
$query->setParameter('id', $id);
$results = $query->getArrayResult();
foreach($results as $i => $result)
{
$productLine = $result['description'];
$modalBoxesHtml .= $this->partial("modal_box.phtml", array(
'id' => $result['id'],
'html' => (new ModalBoxFactory())->getHtmlMaker($productLine)->getHtml($result)
));
}
return $modalBoxesHtml;
}
/**
* Singleton pattern that creates EntityManager instance using Doctrine API
*
* @return \Doctrine\ORM\EntityManager
*/
public function getEntityManager()
{
return DoctrineConnector::getEntityManager();
}
My question is -- I am not too happy about how I use my persistence layer, in my case Doctrine ORM. Should I use it inside my method? Should it be elsewhere? I vaguely recall there is a way to encapsulate and place it better, but how I don't know.
But what I mean, for example, that instead of calling Doctrine directly, i.e my code should look like this...
$results = $(magic...)->getValuesForSalesItem(...);
Namely, I should have some kind of a layer that has a class with a properly-named (business-sense making) method name. In my case it will be "getting values for a specific sales item" which I then pass to the view. But even that is that a good way to do it?
In essence, I seek better placement of my persistence layer ORM-related code within the greater structure of MVC in my application.
Because I am not happy the way it is done now where I call my EntityManager
object and the DQL statement right in the Action
method. Action
== ZF2 speak for method
of a Controller
class that responds to a UI-instantiated event.