【问题标题】:symfony - admin module filters accessible as linkssymfony - 管理模块过滤器可作为链接访问
【发布时间】:2011-04-15 15:58:28
【问题描述】:

我正在开发一个管理仪表板,它将主要基于使用 sfDoctrineGuardPlugin 登录的当前用户

不过,我想做的是在仪表板中添加链接,这些链接基本上是其他模块的过滤器。

问题是,我不知道该怎么做。

例如,我希望将以下内容作为链接:

  • 列出新用户 - 此链接需要列出过去 30 天内添加的所有用户
  • 列出供应商 - 这需要列出属于 group_id 为 2 的组的所有用户
  • 列出制造商 - 这需要列出 group_id 为 3 的组中的所有用户

我该怎么做呢?

谢谢

【问题讨论】:

  • 是所有这些不同的模块还是一个,管理员生成器?
  • 这些链接位于主管理页面仪表板。链接需要过滤sfDoctrineGuard中的用户,sfGuardUser

标签: symfony1 symfony-1.4


【解决方案1】:

我真的不认为你应该在你的情况下使用过滤器。过滤器是数据列表的临时条件。 这是一个更优雅的解决方案。我们将重用 sfGuardUser 索引操作的功能,并“即时”设置它的 table_method(基于 url)。

//exetent the configuration class to override getTable method
class sfGuardUserGeneratorConfiguration extends BaseSfGuardUserGeneratorConfiguration
{
  protected $tableMethod = null;

  public function setTableMethod($name)
  {
    $this->tableMethod = $name;
  }

  public function getTableMethod()
  {
    return null !== $this->tableMethod ? $this->tableMethod : parent::getTableMethod();
  }
}

//now we need to set the tableMethod based on a route param (list):
class sfGuardUserActions extends autoSfGuardUserActions
{
  public function executeIndex(sfWebRequest $request)
  {
    //create a mapping between an url and table method
    $map = array(
      'clients' => 'getClientsList',
      'suppliers' => 'getSuppliersList',
      'manufacturers' => 'getManufacturersList',
    );
    $list = $request->getParameter('list');
    $table_method = isset($map[$list]) ? $map[$list] : null;
    $this->configuration->setTableMethod($table_method);
    parent::executeIndex($request);
  }
}

//create a custom url for your lists:
sf_guard_user_list:
  url:   /guard/users/:list
  param: { module: sfGuardUser, action: index}
  requirements:
    list: clients|suppliers|manufacturers

//and model methods for each of your lists:
class sfGuardUserTable extends PluginsfGuardUserTable
{
  /**
   * List of clients query
   *
   */
  public function getClientsList()
  {
    $q = $this->createQuery('u')
      ->leftJoin('u.Groups g')
      ->where('g.name = ?', 'client');

    return $q;
  }
  //and others
}

就是这样。现在您可以像这样向仪表板添加链接:

<?php echo link_to('Clients', 'sf_guard_user_list', array('list'=>'clients')) ?>

附:这种方法现在允许您在这些列表之上使用过滤器(出于真实原因)。但是,您还必须调整适当的链接。

【讨论】:

  • 太棒了!我今天会看看这个,看看它是怎么回事。这几乎是我想要的,只是链接,根据他们的组显示用户列表
  • 好的,我已经尝试与用户一起使用并且效果很好。但是对其他模块使用相同的代码是行不通的。例如,我在orders 表上尝试过,但它总是返回 5 条记录,而不是基于我的模型方法的特定记录。任何想法为什么会这样?
  • 似乎$this-&gt;configuration-&gt;setTableMethod($table_method);返回null,但我不知道为什么...
  • 真的不能说,检查你是否扩展了正确的类。 setTableMethod 不应该返回任何东西。
  • 我扩展了我的管理模块在构建管理模块时创建的配置文件。它似乎确实调用了代码,但它返回列表中的所有结果,而不是它应该返回的 2 条记录。我检查了开发工具栏并运行了我的方法创建的 SQL,它返回 2。只是不在列表中
【解决方案2】:

这里有一个问题:管理员生成器将所有过滤器数据记住为用户的属性(读作“在会话中存储此信息”)。

因此,如果您不关心过滤器的所有记忆数据,您可以创建一个接收 GET 参数的模块,将此参数设置为用户属性 (sfUser->setAttribute(...)) 覆盖所需模块的过滤器数据,然后重定向到模块。

或者

您可以使用 GET 参数将它们添加到覆盖过滤器参数的模块 URL (example.com/users?filter[group_id]=123)。在这种情况下,您应该在每个需要的模块中处理这些信息。

【讨论】:

  • 你能给我举个例子吗:users?filter[group_id]=1 不返回任何东西
猜你喜欢
  • 2011-09-05
  • 2016-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-28
  • 2012-10-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多