如何“订购”管理生成器中的 sfWidgetFormDoctrineChoice
我正在使用 Symfony 1.4 和 Doctrine。
假设我有 2 个类别:品牌和产品。
当我在基于管理生成器的管理中创建新产品时,我想从下拉列表中选择一个品牌。 管理生成器正在为我做这件事,自动创建一个 sfWidgetFormDoctrineChoice
。
问题是品牌是按 id 排序的。我希望它们按“标签”字段排序。
为了做到这一点,我在我的 ProductForm
类中执行了以下操作:
$this->widgetSchema['brand_id']->addOption('order_by','label');
但出现以下错误:
语法错误或访问冲突:1064 您的 SQL 语法有错误; 检查对应的手册 您的 MySQL 服务器版本 在行的“a”附近使用正确的语法 1. 查询失败:“SELECT b.id AS b__id, b.external_id AS b__external_id, b.label AS b__label, b.created_at AS b__created_at, b.updated_at AS b__updated_at FROM 品牌 b 订购 l a"
order by 语句真的很奇怪。我不明白为什么它似乎削减了 order by 语句的名称。
编辑: 显然 'order_by' 选项期待一个数组作为第二个参数需要什么值?
I'm using Symfony 1.4 and Doctrine.
Let's say I have 2 classes : a Brand and a Product.
When I create a new product in the Admin Generator based admin, I'd like to choose a brand from a dropdown list.
The Admin Generator is doing that for me, automatically creating a sfWidgetFormDoctrineChoice
.
The problem is that the brands are ordered by id. I'd like them to be ordered by their "label" field.
In order to do that I did the following in my ProductForm
class:
$this->widgetSchema['brand_id']->addOption('order_by','label');
But I get the following error:
Syntax error or access violation: 1064
You have an error in your SQL syntax;
check the manual that corresponds to
your MySQL server version for the
right syntax to use near 'a' at line
1. Failing Query: "SELECT b.id AS b__id, b.external_id AS
b__external_id, b.label AS b__label,
b.created_at AS b__created_at,
b.updated_at AS b__updated_at FROM
brand b ORDER BY l a"
The order by statement is really weird. I don't understand why it seems to cut the name of the order by statement.
Edit: Apparently the 'order_by' option is expecting an array as a second parameter. What values does it expect?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我没有尝试本伦利的解决方案,因为当我找到我的解决方案时他回答正确。这似乎比我最后做的事情更乏味。
我查看了源代码以了解这一切是如何工作的。
事实证明,“order_by”选项需要一个数组来指定要对结果进行排序的字段以及“asc”或“desc”:
它就像一个魅力。
I didn't try benlumley's solution since he answered right when I found my solution. It seems more tedious than what I ended doing.
I took a look at the source code to figure out how all this was working.
It turns out the "order_by" option needs an array specifying the field on which one wants to order the results and either 'asc' or 'desc':
It works like a charm.
您应该看看这里:
http://trac.symfony-project.org/wiki/HowtoSortAdminGeneratorListByForeignTableName
它基于旧版本的 symfony,因此怀疑它链接的插件无法工作。但我认为该方法应该仍然是合理的 - 关键在于您必须向操作添加方法来拦截并修改按此特定字段排序的默认处理:
对于原则,您需要定义/覆盖 addSortQuery,以推进,添加排序标准。
建议您查看缓存文件夹,看看自动生成的类是什么样子,以了解它的工作原理。
You should take a look here:
http://trac.symfony-project.org/wiki/HowtoSortAdminGeneratorListByForeignTableName
Its based off of an old version of symfony, so suspect the plugin it links won't work. But I think the method should still be sound - crux of it is that you have to add method to the action to intercept and amend the default handling of sorting by this specific field:
For doctrine, you need to define/override addSortQuery, for propel, addSortCriteria.
Recommend you take a look in the cache folder to see what the auto-generated classes look like to get the hang of how it works.