php - Symfony 1.4 动态模板

标签 php mysql templates symfony1 module

我对 Symfony 很陌生,正在开发 1.4 版本的应用程序。我可以对我正在研究的某些逻辑使用一些输入,并希望这里有人能给我指出正确的方向。

目前,我正在开发一个简单的搜索模块(不是用于 jobeet 或使用 zend search 的模块),该模块使用用户输入的一些文本查询多个表。用户输入的文本可以在一个或多个中找到三个表:元素、任务、NPC。所有找到的结果将显示在搜索模块的搜索操作中。

我想要的是,结果在搜索操作中显示为正确模块(分别为项目、任务、Npc)的链接,但前提是找到该类型的结果。示例(如果找到任务和元素匹配,但未找到 NPC):

 Your search found 1 Item:
 Item 1

 Your search found 1 quest:
 Quest 1

由于没有找到 NPC,所以甚至不需要告诉用户没有,因此被省略。这就是我遇到麻烦的地方。我不太确定如何去做这件事。我可以逃避并使用 searchSuccess.php 中的 if 语句,并且仅在数组的 count() 大于 1 时才显示这些语句,但这违背了 mvc 的目的,对吗?这是实现这一目标的唯一合乎逻辑的解决方案,还是有其他我没有看到的方法?

我非常感谢任何和所有反馈。

最佳答案

有很多方法可以做到这一点,最简单的可能是这样的:

// controller
public function executeSearch(sfWebRequest $request)
{
   $this->results = array();
   // well assume you are using sfForm and have validated the search query
   // which is $searchTerm and that each of your tables has a search method

   // well also assume youre using object routes for these models
   $this->actionMap = array(
      'Npc' => 'npc_show',
      'Quest' => 'quest_show',
      'Item' => 'item_show'
   );

   foreach(array_keys($this->actionMap) as $model)
   {
      $modelResults = Doctrine_Core::getTable($model)->search($searchTerm);
      if($modelResults)
      {
         $this->results[$model] = $modelResults;
      }
   }

   return sfView::SUCCESS;  
}

因此,我们看到的 View 是 $results 一个多维数组,其中包含查询返回结果的模型的顶级元素。没有任何匹配结果的模型将被省略。 $actionMap 保存一个 ModelName => Routename 映射数组。

// in your searchSuccess

<?php if(count($results)): ?>
   <?php foreach($results as $model => $modelResults): ?>
       <?php printf("<h3>Your search found %s %s results:</h3>", $modelResults->count(), $model); ?>
       <ul>
          <?php foreach($modelResults as $result): ?>
             <li><?php echo link_to($result, $actionMap[$model], $result); ?></li> 
          <?php endforeach; ?>
       </ul> 
   <?php endforeach; ?>
<?php else: ?>
   <h3>No results found.</h3>
<?php endif; ?>

关于php - Symfony 1.4 动态模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9543425/

相关文章:

php - MySql 查询使用两个带内连接的表(我认为)

c++ - 无法推断 'TYPE' 的模板参数

templates - 寻找用于 `is_allocator` 的 `enable_if` 类型特征

perl - 如何覆盖 Template Toolkit 模板文件中的 WRAPPER?

未找到 PHP 初学者列 1054 未知列

php - 用于 PHP 的 Richtext 编辑器 - MySQL Web 应用程序

php - Laravel - 在多对多表关系中按主值排序

MySQL 忽略索引问题

php - 表单验证在 php mysql 页面中不起作用,在没有表单验证的情况下移动到表单数据处理 php 文件上?

一张表的 MySQL UNION 查询 + ORDER BY