0

I am trying to find the correct place in my code to place database-reading code, and the correct way to pass the data that's been read in the database to the receiving object. Below is my code. Note the $metaData variable. That is the variable in question (it receives data from DB, and it is later passed to the receiving object).

class Controller
{
    // generages PDF with data placed in specific areas of PDF
    public function generateOutline($outputData)
    {
        // PDF library engine
        $pdfEngine = new PdfEngine();

        // Doctrine ORM Database Connector
        $em = DoctrineConnector::getEntityManager();

        // object containing metadata describing where to place data for $outputData
        $metaData = $em->getRepository(Metadata::class)->findAll();

        //method that places $outputData as described by $metaData
        $pdfEngine->generateOutline($metaData, $outputData);
    }
}

// part of View, because PDF is displayed to us
class PdfEngine
{

    public function generateOutline(Metadata $metaData, array $data)
    {
        $this->placeTextOnPdf($metaData, $data);
    }
}

Question...

I am having trouble figuring out where to place $metaData constructs. You see. .. $metaData is the one variable being read from a database (in my code)

Can I place Database into View? Not really. View should not concern itself with details of the database.

Can I place Database into Controller? Not really it should be "skinny controller, fat model"

DO I perchance place it into Model? Where is the model what is it, where do I place my code?

Dennis
  • 7,907
  • 11
  • 65
  • 115
  • `$metaData` should stay exactly where it is. I fail to see how this is a problem. – Andrei Dec 11 '15 at 15:56
  • perhaps..... before that `$metaData` was in the View (`PdfEngine`). I moved it outside of it and passed it to the View instead. I was concerned that Controllers must not read from database directly like I did here. That database-reading must be done in a model somewhere. – Dennis Dec 11 '15 at 15:57
  • Perhaps this question would suit better on codereview.stackexchange.com – Matheno Dec 11 '15 at 15:57
  • As far as I see it you're not reading from the db directly. You are using Doctrine, overall it does wrap the calls to the db. However, you're not wrong. It isn't placed well there, you could move it to a model. – Andrei Dec 11 '15 at 16:00
  • what is a model and where could it fit in my code? – Dennis Dec 11 '15 at 16:06
  • What is the purpose of this refactoring? Is this your real code? I would not bother with this kind of question with a four-line controller. – Ihor Burlachenko Dec 11 '15 at 16:17
  • it is my real code. I simplified it a bit with `PdfEngine` (there was a factory figuring out the proper PdfEngine to pull) and removed code I deemed irrelevant. Purpose is to future-proof the code to ease maintenance, to ease future technical debt. You do not have to bother. – Dennis Dec 11 '15 at 16:19
  • imho, it should be in the services; controller should be handling on altering the view based on request – Andrew Dec 11 '15 at 16:34
  • I have this post bookmarked, maybe it can help http://stackoverflow.com/questions/5863870/how-should-a-model-be-structured-in-mvc – Andrew Dec 11 '15 at 18:40
  • I have been reading that post for a bit.. So far I am creating a Service Layer that does processing and also rendering of the PDF. Not sure if that does the right job, but at least it moves out the ORM code to be outside of the controller. I probably will wreck havoc to the code before it stabilizes. – Dennis Dec 11 '15 at 18:42
  • I have been trying to build a MVC myself...just to learn a few here and there, but it so complex mixing with multi concept and pattern...im nearly giving up...from what I understand is that the service is like a intermediate between storage and domain object, so that the business logic will all go into model layer and wont "leak" and left the controller layer, which form a "skinny controller and fat model" – Andrew Dec 11 '15 at 18:54

1 Answers1

0

I ended up doing something like this..

class Controller
{
    public function generateOutline($input)
    {
        $data = $this->inputFilter->getOutlineRequestData($input);

        //leaving around some existing processing code
        //may have to find a better place for it later
        $processor = (new OutlineProcessorFactory())->getProcessor($input['line']);
        $output = $processor->processInput($data);

        //encapsulated my code in a "service" layer
        $this->pdfService->renderPdf($output);
    }
}

class PdfService()
{
     function renderPdf($output);
     {    
        $pdfEngine = new PdfEngine();
        $em = DoctrineConnector::getEntityManager();
        $metaData = $em->getRepository(Metadata::class)->findAll();
        $pdfEngine->generateOutline($metaData, $outputData);
     }
}
Dennis
  • 7,907
  • 11
  • 65
  • 115