2012-06-06 3 views
0

Работа с Propel ORM 1.5, мне не хватает метода для объединения двух PropelCollections.Propel - сбор слияний

Краткое предложение может быть:

public function mergeCollection($collection){ 

    foreach($collection as $i => $item){ 
     if(! $this->contains($item)){ 
      // append item 
      $this->append($item); 
     } 
    } 
} 

Так что я новичок в Propel Я хотел бы спросить вас, если есть лучший способ сделать это?
Или эта функциональность уже включена в Propel, но я ее еще не обнаружил?

ответ

1

В списке рассылки discutedtwice, но я не могу найти билет.

По крайней мере, вы можете попробовать этот код и/или open a ticket on Github.

/** 
    * Add a collection of elements, preventing duplicates 
    * 
    * @param  array $collection The collection 
    * 
    * @return int the number of new element in the collection 
    */ 
    public function addCollection($collection) 
    { 
     $i = 0; 
     foreach($collection as $ref) { 
      if ($this->add($ref)) { 
       $i = $i + 1; 
      } 
     } 
     return $i; 
    } 

    /** 
    * Add a an element to the collection, preventing duplicates 
    * 
    * @param  $element The element 
    * 
    * @return bool if the element was added or not 
    */ 
    public function add($element) 
    { 
     if ($element != NULL) { 
      if ($this->isEmpty()) { 
       $this->append($element); 
       return true; 
      } else if (!$this->contains($element)) { 
       set_error_handler("error_2_exception"); 
       try { 
        if (!method_exists($element, 'getPrimaryKey')) { 
         restore_error_handler(); 
         $this->append($element); 
         return true; 
        } 
        if ($this->get($element->getPrimaryKey()) != null) { 
         restore_error_handler(); 
         return false; 
        } else { 
         $this->append($element); 
         restore_error_handler(); 
         return true; 
        } 
       } catch (Exception $x) { 
        //il semble que l'element ne soit pas dans la collection 
        restore_error_handler(); //restore the old handler 
        $this->append($element); 
        return true; 
       } 
       restore_error_handler(); //restore the old handler 
      } 
     } 
     return false; 
    } 

} 

function error_2_exception($errno, $errstr, $errfile, $errline,$context) { 
    throw new Exception('',$errno); 
    return true; 
} 
+0

Хорошо, я нашел этот код, выполнив поиск в списке рассылки. Это похоже на способ сделать это! У вас есть идея о том, как интегрировать эти функции в существующий проект. Предотвращение изменения класса PropelCollection вручную? – domi27

+0

Ну, это будет усложнять, я предполагаю, что интегрирую эти функции без изменения PropelCollection (может быть временное исправление, ожидающее объединения патча). И как Франсуа говорит на ML _write патч с модульными тестами и публикует его на Propel Github, тогда ваша функция может оказаться в следующей версии Propel_. – j0k

Смежные вопросы