0

Working with Propel ORM 1.5, I'm missing a method to merge two PropelCollections.

A short proposal may be :

public function mergeCollection($collection){

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

So I'm new to Propel I would like to ask you, if there are better ways to do it ?
Or is this functionality already included in Propel, but i didn't yet discovered it ?

j0k
  • 22,600
  • 28
  • 79
  • 90
domi27
  • 6,903
  • 2
  • 21
  • 33

1 Answers1

1

It seems to have been discuted twice in the mailing list, but I can't find the ticket.

At least, you can try this code and/or 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;
}
j0k
  • 22,600
  • 28
  • 79
  • 90
  • Ok, i found this code also by searching the mailing list. This seems the way to do it ! Do you have an idea, on how to integrate these functions in an existing project - Preventing to alter the PropelCollection class manually ? – domi27 Jun 10 '12 at 08:30
  • Well, it will be to complicate I guess to integrate these functions without altering the PropelCollection (could be a temporary fix waiting for the patch to be merged). And as Francois says on the ML _write a patch with unit tests, and publish it on the Propel Github, then your function could end up in the next Propel version_. – j0k Jun 10 '12 at 09:59