3

Let's say I have a Comments model that has a field named text that can store formatted (html) text

Most of the times this text comes from a form and it's saved as usual calling Comments->patchEntity() and so on...

Sometimes instead I have auto-generated Comments. Now I am creating that content in the controller. Something like this (note that this is an over semplificate example):

 // PostsController


 public function view($id)
 {
     //
     //
     // if Post views are >= 1 billion 

     $comment = $this->Comments->newEntity();
     $comment->text = "<strong>Congratulations!</strong> your Post just    
               hit 1 billion views! This is an auto generated 
               comment";
     $this->Posts->Comments->save($comment);
 }

I know that this is wrong and it's against MVC pattern.

Also I can't use HtmlHelper in controllers so if I have to create html tags by hand.

I've seen this question but it does not seem a clean approach (also it's for cake 2)

So: what is the cleaner way to achieve what I'm already doing without breaking MVC?

Additional info

the question is in general about how to save a generated (HTML) content to the DB using cakephp 3. The above is just an example: I can move that code to the afterSave method or create a listener. I can use an Element or a Helper to generate the content (or more likely both)

I think there should be a way similar to the way Email class does: I can call Email() wherever I want in my code (controllers, models, listeners) and generate the email body using templates. I would like to do the same and save to DB

Why do I need this?

Maybe this is a XY problem so here's my actual issue

I have a very complex view that depends on the records stored in many Tables. I want to track the history of this view every time there is a change, so my idea is to create a listener, attach this listener to every table involved in the creation of the view and save a snapshot of the view every time there is an update

arilia
  • 9,303
  • 2
  • 18
  • 40
  • Generally this looks like something that should happen in an `afterSave` event of the `Posts` model. Do you really need to use the helper? In any case you could always create a view builder wherever you want. – ndm Jun 22 '17 at 22:17
  • @ndm, yes, the next step is to move the code in the afterSave event or maybe I will create an eventListerer, since this kind of auto generated comments could happen in many models. The comment text often contains links, so the Html helper would be useful but I can do without it. Can you please explain about ho to create a viewBuilder? Thanks in advance – arilia Jun 23 '17 at 07:22
  • You can create a view builder everywhere by simply instantiating `\Cake\View\ViewBuilder`, or if appropriate, using `\Cake\View\ViewVarsTrait::viewBuilder()`. – ndm Jul 16 '18 at 14:31

1 Answers1

0

So, following @ndm suggestion here's what I did

 $vars = [/* my viewVars here */]
 $view = new ViewBuilder();
 $content = $view->setTemplate("path_to_my_template")
    ->setLayout(false) // I need an empty layout
    ->setHelpers(['Number'])  // I need some additional helper
    ->build($vars)  //here I pass my vars to the template
    ->render();

In this way I can get my generated content. I can use the above code in a model, or in a event listener. i.e. in a Model I can do

$entity = $this->newEntity():
$entity->content = $content;
$this->save($entity);
arilia
  • 9,303
  • 2
  • 18
  • 40