Search code examples
symfony1doctrinesymfony-1.4admin-generator

How to "order by" a sfWidgetFormDoctrineChoice in the Admin Generator


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?


Solution

  • 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':

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

    It works like a charm.