我应该在控制器或视图(CakePHP)中转换时间戳吗?
我正在努力了解 MVC 的最佳实践(使用 CakePHP),并尝试了解某个任务是否应该在控制器或视图中发生。
场景如下:
我有一个用户表。每个用户都有许多与其相关的事件。
在我的控制器中,我将有关用户的内容加载到数组中,如下所示:
$this->set('user', $this->User->read());
这会产生一个我可以在视图页面上循环的用户数组:
Array
(
[User] => Array
(
[id] => 1
[name] => Jane Doe
[created] => 2011-03-29 15:50:25
[modified] => 1301428225
)
[Event] => Array
(
[0] => Array
(
[id] => 4
[user_id] => 1
[title] => Birthday
[created] => 2011-04-07 17:28:53
[modified] => 2011-04-07 17:28:53
[occured] => 1301889600
)
[1] => Array
(
[id] => 3
[user_id] => 1
[title] => Anniversary
[created] => 2011-04-07 17:21:27
[modified] => 2011-04-07 17:21:27
[occured] => 1301976000
)
[2] => Array
(
[id] => 2
[user_id] => 1
[title] => Graduation
[created] => 2011-04-07 17:20:41
[modified] => 2011-04-07 17:20:41
[occured] => 1301889600
)
)
现在,发生的是一个时间戳,我需要将其转换为友好的日期。
我应该:
A) 在控制器中执行此操作吗?如果是这样,挖掘数组并执行此操作的最佳语法是什么?
B)调用时在视图中执行它吗?
<?=date("d/m/Y,$thisEvent['occured']);?>
后者看起来更干净,代码更少,但我不知道我是否可以/应该从控制器应用它的逻辑。
I'm working at understanding best practices for MVC (using CakePHP) and am trying to understand if a certain task should be happening in the controller or the view.
Here's the scenario:
I have a table of users. Each user has many events associated with them.
In my controller I'm loading content about a user into an array like so:
$this->set('user', $this->User->read());
That results in a user array I can loop through on my view page:
Array
(
[User] => Array
(
[id] => 1
[name] => Jane Doe
[created] => 2011-03-29 15:50:25
[modified] => 1301428225
)
[Event] => Array
(
[0] => Array
(
[id] => 4
[user_id] => 1
[title] => Birthday
[created] => 2011-04-07 17:28:53
[modified] => 2011-04-07 17:28:53
[occured] => 1301889600
)
[1] => Array
(
[id] => 3
[user_id] => 1
[title] => Anniversary
[created] => 2011-04-07 17:21:27
[modified] => 2011-04-07 17:21:27
[occured] => 1301976000
)
[2] => Array
(
[id] => 2
[user_id] => 1
[title] => Graduation
[created] => 2011-04-07 17:20:41
[modified] => 2011-04-07 17:20:41
[occured] => 1301889600
)
)
Now, occured is a timestamp which I need to convert to a friendly date.
Should I:
A) Do this in the controller? If so, what's the best syntax to dig into the array and do that?
B) Do it in the view when it's called?
<?=date("d/m/Y,$thisEvent['occured']);?>
The latter seems cleaner with less code, but I don't know if it's logic I can / should be applying from the controller.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您要在视图中循环遍历数组以显示事件,我只需将转换放在视图中即可避免在控制器中循环。
对我来说,日期格式是一个表示的事情,而不是一个业务逻辑的事情。
艾米
If you are going to loop through an array in the view anyway in order to display the events, I would just put the conversion in the view to save having to loop in the controller as well.
To me, the date formatting is a presentation thing - not a business logic thing.
Amy
经验法则是:胖模型到瘦视图。视图不应该处理逻辑。时期。视图不应该处理数据的更改,它应该只处理显示数据。模型是数据操作应该发生的地方。但有时作为程序员,我们希望采取最简单的方式来做事。这不一定是错误的,但并不总是最好的做事方式。
我的建议是以下之一(优先顺序):
1- 更改数据库上的日期格式,使其以您正在寻找的格式显示。这样就不需要额外的逻辑来格式化日期。
2- 将查询编写为联接。 (由于递归已开启,因此在这种情况下您应该执行此操作)。编写联接,以便返回的数据格式符合您的预期。问题是您使用 $this->User-Read() 走捷径,这导致您必须在模型外部编写额外的逻辑来处理日期的格式。 Model->read() 有它的用途,这不是其中之一。
3-如果您想使用 Model->read() 快捷方式,则可以构建一个日期助手,您可以从任何给定日期的视图中引用该助手。例如,当您在视图中显示字段时,您可以调用:
Date->format($thisEvent['occurred']); ?>
那么助手就是包含代码的位置,如下所示:
这样,如果您决定要更改日期的外观,则只需更改助手(1 个位置)而不是所有视图(多个位置)。
The rule of thumb is this: Fat Model to Skinny View. The View should not handle logic. Period. The view should not handle alteration of data, it should only handle displaying it. The Model is where data manipulation should take place. But sometimes as coders we want to take the most easy way of doing things. Which isn't necessarily wrong, but isn't always the best way of doing thing.
My advice is one of the following (order of preference):
1- Alter the date format on the database so it comes out in the format you are looking for. Then there is no extra logic required for the formatting of the date.
2- Write the query as a join. (Which is what you should be doing in this case since recursive is on). Write the join so that the data comes back formatted as you expect. The problem is you are taking the shortcut by using $this->User-Read() which is causing you to have to write additional logic outside of the model to handle the formatting for the dates. The Model->read() has it's uses, this is not one of them.
3- If you are bent on using the Model->read() shortcut, you can build a date helper that you can reference from the view for any given date. For example, when you display the field in the view, you would call:
<?php echo $this->Date->format($thisEvent['occured']); ?>
Then the helper is where you would contain the code as follows:
This way, if you ever decide you want to change the look of the dates, you only have to change the helper (1 location) instead of all of the views (multiple locations).
我建议:
模型返回
DateTime
对象或类似对象。这样,在必要时可以很容易地在控制器或视图中对日期进行计算/格式化(良好的可维护性)。正如另一个答案中所述,视图应尽可能保持简单(可读性)。通过 Smarty + 处理特定的 DateTime 对象,我可以简单地在 Views 中编写
{$someItem.someDate}
,并且日期会自动格式化(根据到当前用户的语言),通过使用我的特定 DateTime 类的__toString()
方法。I would suggest that:
the Models return
DateTime
objects or similar. This way, it can be easy in the Controllers or the Views to make calculations/formatting on dates when necessary (good maintainability).the Views, as said in another answer, should be kept as simple as possible (readability). With Smarty + working with specific DateTime objects, I can simply write
{$someItem.someDate}
in Views and the dates are formatted automatically (according to current user's language), by using a__toString()
method of my specific DateTime class.