2012-06-06 2 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에 포함되어 있지만 아직 발견하지 못했습니다.

답변

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을 변경하지 않고 이러한 함수를 통합하는 것이 복잡 할 것입니다 (패치가 병합되기를 기다리는 임시 수정 일 수 있음). Francois가 ML에서 unit 테스트로 패치를 작성하고 Propel Github에 게시하면 함수가 다음 Propel 버전으로 끝날 수 있습니다 _. – j0k