data-import icon indicating copy to clipboard operation
data-import copied to clipboard

DoctrineWriter array index

Open balazscsaba2006 opened this issue 11 years ago • 1 comments

Hi,

First of all, I would really like to thank you for this great library, and this is why I would like to propose the extending of the DoctrineWriter, to accept also an array for the index, to be able to find entities by multiple clauses.

I've extended the DoctrineWriter's writeItem() method like this:

if (false === $this->truncate) {
    if ($this->index) {
        $fields = array();
        if (is_array($this->index)) {
            foreach ($this->index as $index) {
                $fields[$index] = $item[$index];
            }
        } else {
            $fields[$this->index] = $item[$this->index];
        }
        $entity = $this->entityRepository->findOneBy($fields);
    } else {
        $entity = $this->entityRepository->find(current($item));
    }
}

This way the index parameter can be an array

$writer = new DoctrineWriter($em, $entityClass, array('field1', 'field2'));

I could try to make a PR if you think this is a feature that should be in the library.

balazscsaba2006 avatar Oct 08 '14 10:10 balazscsaba2006

I must say absolutely yes, in my case I came across the same issue today, as the lib was forcefully trying to use the first column of my file as the ID, and because I have no ID I needed to check against multiple values

emudojo avatar Apr 06 '16 15:04 emudojo