如何添加“热门事件/最近更新”部分到使用Symfony 1.4构建的网站


How to add a "Top happenings/Recent Updates" section to a website built with Symfony 1.4?

我正在开发一个网站symfony 1.4。用户上传照片,照片保存在数据库中。

现在在主页上,我有一些内容,我想有一个部分显示最近上传的照片。我不确定做这件事的最好方法是什么。你能给我指个正确的方向吗?

我能想到的唯一方法是让$sf_content保持主要内容(这将来自应用程序/模块),并为最近的上传,让layout.php做数据库访问+业务逻辑+渲染,但这将违反MVC,将涉及大量的数据库访问,每次用户在网站导航。

可以做得更好吗?

可以使用组件

应用程序/yourapp/模块/yourmodule/行动/components.class.php

class yourmodulenameComponents extends sfComponents {

    public function executeRfoto() {
        $this->photos = Doctrine_Core::getTable('Photo') ->getRecentPhotos();
    }
在<<p> strong> :
public function getRecentPhotos()
  {
    $q = $this->createQuery('a')
              ->addORDERBY('DESC');
    return $q->execute();
  }

apps/yourapp/modules/yourmodule/themplates/_rfoto.php

    foreach ($photos as $photo){
// Make some stuff
}

layout.php,或者其他你想放照片的地方:

   <?php include_component('yourmodule' ,'rfoto')?>