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