Development

#1751 ([PATCH] Use different field (not primary key) as option value)

You must first sign up to be able to contribute.

Ticket #1751 (closed enhancement: fixed)

Opened 1 year ago

Last modified 4 months ago

[PATCH] Use different field (not primary key) as option value

Reported by: Serg.Kalachev Assigned to: FabianLange
Priority: major Milestone: 1.1.0 RC2
Component: helpers Version: 1.0.4
Keywords: Object Cc:
Qualification: Accepted

Description

This enhancement allow developer to use different field (not primary key) as option value in object_select_tag.

1) Change at line 120

<?php
...
  $text_method = _get_option($options, 'text_method');

  $select_options = _get_options_from_objects(sfContext::getInstance()->retrieveObjects($related_class,
 $peer_method), $text_method);
...

to

<?php
...
  $text_method = _get_option($options, 'text_method');
  $key_method = _get_option($options, 'key_method');
  $select_options = _get_options_from_objects(sfContext::getInstance()->retrieveObjects($related_class, 
$peer_method), $text_method, $key_method);
...

2) Change at line 155(151)

<?php
...
function _get_options_from_objects($objects, $text_method = null)
{
  $select_options = array();

  if ($objects)
  {


    // multi primary keys handling
    $multi_primary_keys = is_array($objects[0]->getPrimaryKey()) ? true : false;

    // which method to call?
    $methodToCall = '';
    foreach (array($text_method, '__toString', 'toString', 'getPrimaryKey') as $method)
...

to

<?php
...
function _get_options_from_objects($objects, $text_method = null, $key_method = null)
{
  $select_options = array();

  if ($objects)
  {
    $getKeyMethod = ($key_method) ? $key_method : 'getPrimaryKey';
    
   // multi primary keys handling
    $multi_primary_keys = is_array($objects[0]->$getKeyMethod()) ? true : false;

    // which method to call?
    $methodToCall = '';
    foreach (array($text_method, '__toString', 'toString', $getKeyMethod) as $method)
...

3) Change at line 180(174)

<?php
...
      $key   = $multi_primary_keys ? implode('/', $tmp_object->getPrimaryKey()) : $tmp_object->getPrimaryKey();
...

to

<?php
...
      $key   = $multi_primary_keys ? implode('/', $tmp_object->$getKeyMethod()) : $tmp_object->$getKeyMethod();
...

Attachments

ObjectHelper.patch (2.0 kB) - added by Serg.Kalachev on 06/21/07 23:14:27.

Change History

06/09/07 10:10:06 changed by fabien

  • milestone deleted.

06/21/07 23:13:51 changed by Serg.Kalachev

  • cc set to fabien.
  • summary changed from Use different field (not primary key) as option value to [PATCH] Use different field (not primary key) as option value.
  • version changed from 1.0.2 to 1.0.4.
  • milestone set to 1.0.5.

06/21/07 23:14:27 changed by Serg.Kalachev

  • attachment ObjectHelper.patch added.

06/22/07 07:19:58 changed by fabien

  • cc deleted.
  • milestone deleted.

Please, don't add me in the CC field and don't put a milestone.

10/29/07 22:57:09 changed by synace

  • qualification set to Unreviewed.

still not fixed in 1.0.9PRE,

my particular use: needed for indentation of nested sets in admin area with double_list selector

05/28/08 14:05:35 changed by FabianLange

  • owner changed from fabien to FabianLange.
  • status changed from new to assigned.
  • qualification changed from Unreviewed to Accepted.
  • milestone set to 1.1.0 RC2.

i think that is pretty useful. The patch is very good and this can be implemented BC. however I think that should go only into 1.1.

05/28/08 14:32:31 changed by FabianLange

  • status changed from assigned to closed.
  • resolution set to fixed.

(In [9340]) 1.1: made the key method configurable for object_select_tag. no BC changes due sensible defaults. closes #1751