【问题标题】:How to sort FK filter in symfony's admin generator using Doctrine?如何使用 Doctrine 在 symfony 的管理生成器中对 FK 过滤器进行排序?
【发布时间】:2012-12-20 21:23:49
【问题描述】:

我有一个 symfony 管理员为我的诗歌课程生成的模块。我的诗歌表对我的 MasterIndex 表 (user_id) 有一个 foreign_key 约束。当使用 Symfony 管理生成器时,在列表模式下,它会为我的 user_id 显示一个下拉过滤器。我可以通过修改 MasterIndex 类中的 __toString() 方法来更改正在显示的数据,以显示用户名而不是 Id。

如何对过滤器下拉菜单中显示的数据进行排序?

我找到了提出解决方案的this 链接,但它仅使用的peer_method 似乎适用于Propel。

我在我的 Symfony 1.4 项目中使用 Doctrine 1.2。

【问题讨论】:

    标签: php symfony-1.4 doctrine-1.2


    【解决方案1】:

    您可以找到sfWidgetFormDoctrineChoice here 的可用选项

    * model:        The model class (required)
    * add_empty:    Whether to add a first empty value or not (false by default)
                    If the option is not a Boolean, the value will be used as the text value
    * method:       The method to use to display object values (__toString by default)
    * key_method:   The method to use to display the object keys (getPrimaryKey by default)
    * order_by:     An array composed of two fields:
                      * The column to order by the results (must be in the PhpName format)
                      * asc or desc
    * query:        A query to use when retrieving objects
    * multiple:     true if the select tag must allow multiple selections
    * table_method: A method to return either a query, collection or single object
    

    您可以像这样使用order_by 选项:...->setOption('order_by', array('some_field', 'asc'))

    更新:

    如果您想按多个字段排序,您可以编写自定义查询并向其添加多个排序依据,然后使用querytable_method 选项而不是order_by

    例如(您应该将查询写入表类):

    $query = Doctrine_Core::getTable('UserTable')->createQuery('u')->orderBy('u.last_name asc, u.first_name asc');
    $this->getWidget('user_id')->setOption('query', $query);
    // if you add some where condition to the query
    // don't forget to set it to the validator as well
    $this->getValidator('user_id')->setOption('query', $query);
    

    【讨论】:

    • 我不确定我是否遵循 - 该代码需要去哪里?我在哪里/如何告诉 generator.yml 中的管理员生成器调用哪个方法以及从哪个类调用?
    • 它应该进入生成的过滤表单类的配置方法。例如。 ` public function configure() { $this->getWidget('user_id')->setOption('order_by', array('some_field', 'asc')) }`
    • 效果很好。德克萨斯州。有没有办法对多个字段进行排序(即:按姓氏 asc、名字 asc、dateOfBirth desc 排序)?我试过查看 sfWidgetFormDoctrineChoice 对象,但它似乎只需要一个参数作为 order_by 参数。如果我指定多个级联 ->setOption('order_by', ...) 它只会使用 sql 语句中的最终级联。
    猜你喜欢
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    • 1970-01-01
    • 2010-11-21
    • 2023-04-09
    相关资源
    最近更新 更多