Ticket #1751: ObjectHelper.patch
| File ObjectHelper.patch, 2.0 kB (added by Serg.Kalachev, 2 years ago) |
|---|
-
ObjectHelper.php
old new 118 118 $peer_method = _get_option($options, 'peer_method'); 119 119 120 120 $text_method = _get_option($options, 'text_method'); 121 122 $key_method = _get_option($options, 'key_method'); 121 123 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); 123 127 124 128 if ($value = _get_option($options, 'include_custom')) 125 129 { … … 148 152 return select_tag(_convert_method_to_name($method, $options), $option_tags, $options); 149 153 } 150 154 151 function _get_options_from_objects($objects, $text_method = null )155 function _get_options_from_objects($objects, $text_method = null, $key_method = null) 152 156 { 153 157 $select_options = array(); 154 158 155 159 if ($objects) 156 160 { 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; 159 165 160 166 // which method to call? 161 167 $methodToCall = ''; 162 foreach (array($text_method, '__toString', 'toString', 'getPrimaryKey') as $method)168 foreach (array($text_method, '__toString', 'toString', $getKeyMethod) as $method) 163 169 { 164 170 if (is_callable(array($objects[0], $method))) 165 171 { … … 171 177 // construct select option list 172 178 foreach ($objects as $tmp_object) 173 179 { 174 $key = $multi_primary_keys ? implode('/', $tmp_object-> getPrimaryKey()) : $tmp_object->getPrimaryKey();180 $key = $multi_primary_keys ? implode('/', $tmp_object->$getKeyMethod()) : $tmp_object->$getKeyMethod(); 175 181 $value = $tmp_object->$methodToCall(); 176 182 177 183 $select_options[$key] = $value;