【问题标题】:Filter by UserProfile fields with sfGuard plugin使用 sfGuard 插件按 UserProfile 字段过滤
【发布时间】:2012-07-19 19:33:53
【问题描述】:

在 Symfony 1.4 中使用 sfGuard 插件时,如何按 sfGuardUserProfile 字段进行过滤?

按照我的标准,我创建了一个 sf_guard_user_profile 表来保存名字、姓氏等字段。

我的管理索引页面有一个相当丰富的用户列表。我可以按用户名过滤就好了。但我无法按 sf_guard_user_profile 表中的字段进行过滤,例如 last_name。

我的 generator.yml 看起来像

generator:
  class: sfPropelGenerator
  param:
    model_class: sfGuardUser

    config:
      list:
        display: [=username, first_name, last_name, last_login, role]
        batch_actions: []

      filter:
        display: [username]

      form:
        class: myGuardUserAdminForm

我可以通过更改为:

filter:
    class: sfGuardUserProfileFormFilter

但是当我提交带有有效 last_name 值的表单时,它不会过滤。返回所有记录。

【问题讨论】:

    标签: symfony1 symfony-1.4 sfguard


    【解决方案1】:

    sfGuardUserProfileFormFilter 添加为过滤器类并不是过滤first_name、last_name 和其他配置文件字段的正确方法。

    您必须在过滤器显示列表中添加一个需要过滤的字段,例如:

    filter:
        display: [username, first_name, last_name]
    

    那么你必须将sfGuardUserProfileFormFilter合并到sfGuardUserFormFilter中:

    public function configure()
    {
        parent::configure();
    
        $this->mergeForm(new sfGuardUserProfileFormFilter());
    }
    

    最后为您要过滤的合并表单sfGuardUserProfileFormFilter 的每个字段添加sfGuardUserFormFilter 函数addMyFieldColumnCriteria(推进符号)。对于 first_name,我们有:

        protected function addFirstNameColumnCriteria(Criteria $criteria, $field, $values)
        {
            if (!is_array($values))
                return;
    
            $criteria->addJoin(sfGuardUserPeer::ID, sfGuardUserProfilePeer::USER_ID, Criteria::INNER_JOIN);
    
            if (isset($values['is_empty']) && $values['is_empty'])
            {
                $criteria->add($field, null, Criteria::ISNULL);
                $criteria->addOr($field, '', Criteria::EQUAL);
            }
            else
            {
                $value = $values['text'];
                if ($value == '')
                    return;
                $criteria->add($field, '%'.$value.'%', Criteria::LIKE);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2010-12-09
      • 1970-01-01
      • 1970-01-01
      • 2016-06-15
      • 1970-01-01
      • 2014-04-21
      • 2014-08-30
      • 2017-06-08
      • 2019-04-15
      相关资源
      最近更新 更多