English 中文(简体)
如何在管理生成器中按“order by”排列sfWidgetFormDoctrineChoice。
原标题:How to "order by" a sfWidgetFormDoctrineChoice in the Admin Generator

我正在使用Symfony 1.4和Doctrine。

请允许我说,我有2个班级:品牌和产品。

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.

问题是,这些品牌是用id子订购的。 我喜欢由他们的“标签”领域命令他们。

为了做到这一点,我在我的ProductForm类中进行了以下操作:

$this->widgetSchema[ brand_id ]->addOption( order_by , label );

但是我得到了以下错误:

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"

排序语句真的很奇怪。我不明白为什么它似乎要截断排序语句的名称。

<><>Edit>: 很明显,按顺序排列的顺序是将一个阵列作为第二个参数。 它期望什么价值?

最佳回答

因为我在找到自己的解决方案时,Benlumley已经回答了,所以我没有尝试过他的解决方案。它似乎比我最终采用的方法更繁琐。 (Note: As an AI language model, I can only provide the simplified Chinese translation)

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 :

$this->widgetSchema[ product_id ]->addOption( order_by ,array( label , asc ));

它像魔法一样有效。

问题回答

你应该来这里看看:

将此翻译为中文: http://trac.symfony-project.org/wiki/HowtoSortAdminGeneratorListByForeignTableName http://trac.symfony-project.org/wiki/HowtoSortAdminGeneratorListByForeignTableName

它基于一个旧版本的symfony,所以怀疑它链接的插件不会起作用。但我认为这个方法仍然是可靠的 - 它的关键是你必须添加方法来拦截和修改按此特定字段排序的默认处理的动作:

对于Doctrine,您需要定义/覆盖addSortQuery,对于Propel,添加addSortCriteria。

建议你看管切片,看看看由汽车制成的班子,看看它如何运作。





相关问题
Doctrine quoting apostrophes

I am using Doctrine ORM for php and Zend MVC. I have a mysql database. When I insert into the database it is escaping quotes. In other-words when I enter <input name="customer_name" value="Test ...

What does mutators and accessors do in Doctrine?

In this document, it explains how to use mutators and accessors in Doctrine, but does not explain what they are. Could anyone explain what mutators and accessors do and what they are? Thanks in ...

Tracking the views of a given row

I have a site where the users can view quite a large number of posts. Every time this is done I run a query similar to UPDATE table SET views=views+1 WHERE id = ?. However, there are a number of ...

Doctrine schema.yml generator

I am pretty new to doctrine. I made two small projects with doctrine for my own but now I am about to create big project for my client. The project will have more than 50 tables. Is there any way of ...

How does Doctrine handle changes to the database schema?

In brief, what happens when you add a column to a table? What happens when you remove one? In more details, suppose you have the following: class User extends Doctrine_Record { public function ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签