For example in the model for this table I check if the value is null and change it to "Not Entered
This is typically view constrained logic. Now you want to render "Not Entered", perhaps in another UI you want to display something else. Keep in mind it is not per se a bad idea to have logic in your view, it should only be view related. Check if a value available and act accordingly is perfectly viable view logic.
I do it that way now for timestamps. I convert timestamps in my model to date strings before they get the view layer (maybe I should be converting the timestamps in my view templates instead?)
Keep the model layer domain driven: use semantics to describe your domain. If you deal with timestamps, PHP's DateTime
class is a very good solution. Use these objects or if not present, set it to null
. This way you keep your model layer together.
A perfect viable signature for the timestamp is:
public function setTimestamp(DateTime $timestamp = null) {
//
}
This way you enforce the variable to be a valid DateTime
instance or you just allow null
.
Your view layer can contain logic such as:
<?php if ($object->getTimestamp() !== null): ?>
<p><?= $object->getTimestamp()->format('c')?></p>
<?php endif ?>