jgrygierek / BatchEntityImportBundle

Importing entities with preview and edit features for Symfony.
MIT License
9 stars 6 forks source link

[TypeError] Allow array as type on import #98

Open rrr63 opened 3 days ago

rrr63 commented 3 days ago

Hi,

I think it could be a good feature to allow array type.

//JG\BatchEntityImportBundle\Model\Configuration\AbstractImportConfiguration
if (method_exists($entity, $setterName)) {
    $reflection = new \ReflectionMethod($entity, $setterName);
    $params = $reflection->getParameters();

    if (!empty($params)) {
        $paramType = $params[0]->getType();
        if ($paramType && $paramType->getName() === 'array') {
            $value = explode('//', $value);
        }
    }
    $entity->$setterName($value);
}

Here is an example with a fixed separator //, but maybe it could be nice to allow user to override it, to use their own separator

jgrygierek commented 3 days ago

It is the simplest case. And it should be easy to allow just 1-dimensional array.

But there is a more advanced case: Do we want only just simple 1-dimensional arrays? Or multidimensional too? Do we want allow only indexes? Or maybe keys too? Maybe better will be store it as json in file?

But currently probably such advanced feature is not needed?

rrr63 commented 3 days ago

I think the 1d array is enough. To test i tried to import some Users, and there is the roles property. Allow simple string or numeric array is already very good and will fix 80% of array usages imo

jgrygierek commented 3 days ago

Ok, I will try to do it maybe this weekend if I will have some time

rrr63 commented 3 days ago

Maybe i can try to do a PR on this

jgrygierek commented 3 days ago

If you want, please do it :)