Oct 31
Digg
Stumbleupon
Technorati
Delicious

How to populate object_select_tag with your own objects

Sometimes you need to have an ability to show limited number of elements in your select objects or sort elements by name or so. I figured out that this feature was added to object_select_tag later so that not everybody knows about that. This feature’s name is ‘peer_method’ – effectively you create your peer method which returns objects which must appear in your object_select_tag. So here is the code for object_select_tag:

echo object_select_tag($domain, ‘getObjectId’, array (
‘related_class’ => ‘Object’,
‘peer_method’ => ‘getSortedObject’,
‘control_name’ => ‘object_id’,
‘include_blank’ => true,
));

and this must be put in your ObjectPeer.class.php:

static public function getSortedObject() {
$c = new Criteria();
$c->addAscendingOrderByColumn(TablePeer::NAME);
$rs = TablePeer::doSelect($c);
return $rs;
}

Interesting how this can be done in generator without re-writing of edit_form.php template. I suspect this can be done directly in generator.yml but never tried it. If someone has experienced would be good to know.

Thanks in advance.


Author: symfonian

13 Comments

Xavier GORSE
November 1, 2007

As simple as this :

fields:
departement_id: { params: text_method =getNomCode peer_method =doSelectOrderByCode }

admin
November 1, 2007

I see you used also additional parameter text_method. Never seen like that, what it’s for?

Xavier GORSE
November 2, 2007

It’s the function used to display the name in the select object

by default the text_method is __toString()

[…] How to populate object_select_tag with your own objects […]

Mauro
December 4, 2007

I am noob in symfony, but i try to learn. my problem right now is that i am population a object_select_tag, but it is always just displaying the id`s of the table in the list. i have also tried to replace the second parameter with another get function, but it still just shows me the id`s in the list… would be happy about some help, thanks.

JuanG
April 1, 2008

Works great, thanks!

Shamsh
November 21, 2008

But how can i grouping the objects.
I mean if i want to use “optgroup” then how can i do that?

mila
March 12, 2009

can I add parameter to the Peer_method like this:
echo object_select_tag($domain, ‘getObjectId’, array (
‘related_class’ => ‘Object’,
‘peer_method’ => ‘getSortedObject(‘+$pays_id+’)’,
‘control_name’ => ‘object_id’,
‘include_blank’ => true,
));

makhz
June 2, 2009

Nice,

Works ok, Thanks a lot.

geoff
March 1, 2010

i know that mila’s method will not work. but is there a good way to pass a paramater to a peer method?

joujouta
April 20, 2010

i want to create a select object, I edited _edit_form but it did not work

Jibu
November 7, 2012

I am new in symfony

where is the file path ObjectPeer.class.php

/lib/model/doctrine/generated/[Filename].class.php ???

to write this code.

static public function getSortedObject() {
$c = new Criteria();
$c->addAscendingOrderByColumn(TablePeer::NAME);
$rs = TablePeer::doSelect($c);
return $rs;
}

Comments RSS TrackBack Identifier URI

Leave a comment