Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
    
yay I got a "Tumbleweed" badge (Asked a question with zero score, no answers, no comments, and low views for a week). How fun. – Dennis Mar 16 at 21:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.