Development

#1751: ObjectHelper.patch

You must first sign up to be able to contribute.

Ticket #1751: ObjectHelper.patch

File ObjectHelper.patch, 2.0 kB (added by Serg.Kalachev, 2 years ago)
  • ObjectHelper.php

    old new  
    118118  $peer_method = _get_option($options, 'peer_method'); 
    119119 
    120120  $text_method = _get_option($options, 'text_method'); 
     121   
     122  $key_method = _get_option($options, 'key_method'); 
    121123 
    122   $select_options = _get_options_from_objects(sfContext::getInstance()->retrieveObjects($related_class, $peer_method), $text_method); 
     124  $select_options = _get_options_from_objects( 
     125   sfContext::getInstance()->retrieveObjects($related_class, $peer_method),  
     126    $text_method, $key_method); 
    123127 
    124128  if ($value = _get_option($options, 'include_custom')) 
    125129  { 
     
    148152  return select_tag(_convert_method_to_name($method, $options), $option_tags, $options); 
    149153} 
    150154 
    151 function _get_options_from_objects($objects, $text_method = null
     155function _get_options_from_objects($objects, $text_method = null, $key_method = null
    152156{ 
    153157  $select_options = array(); 
    154158 
    155159  if ($objects) 
    156160  { 
    157     // multi primary keys handling 
    158     $multi_primary_keys = is_array($objects[0]->getPrimaryKey()) ? true : false; 
     161    $getKeyMethod = ($key_method) ? $key_method : 'getPrimaryKey'; 
     162     
     163   // multi primary keys handling 
     164    $multi_primary_keys = is_array($objects[0]->$getKeyMethod()) ? true : false; 
    159165 
    160166    // which method to call? 
    161167    $methodToCall = ''; 
    162     foreach (array($text_method, '__toString', 'toString', 'getPrimaryKey') as $method) 
     168    foreach (array($text_method, '__toString', 'toString', $getKeyMethod) as $method) 
    163169    { 
    164170      if (is_callable(array($objects[0], $method))) 
    165171      { 
     
    171177    // construct select option list 
    172178    foreach ($objects as $tmp_object) 
    173179    { 
    174       $key   = $multi_primary_keys ? implode('/', $tmp_object->getPrimaryKey()) : $tmp_object->getPrimaryKey(); 
     180      $key   = $multi_primary_keys ? implode('/', $tmp_object->$getKeyMethod()) : $tmp_object->$getKeyMethod(); 
    175181      $value = $tmp_object->$methodToCall(); 
    176182 
    177183      $select_options[$key] = $value;