Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle relationships better #5

Open
simonhamp opened this issue Apr 22, 2019 · 6 comments
Open

Handle relationships better #5

simonhamp opened this issue Apr 22, 2019 · 6 comments
Labels
enhancement New feature or request
Milestone

Comments

@simonhamp
Copy link
Owner

For BelongsTo relationships it would be great to be able to make the relationship where the name matches an existing related resource, even to have the option to create records that don't exist.

At the moment, you have to know the ID of the related resource and update your CSV before you upload it.

@simonhamp simonhamp added the enhancement New feature or request label Apr 22, 2019
@simonhamp simonhamp added this to the v1.0.0 milestone Apr 22, 2019
@richardjkeys
Copy link

@simonhamp This would be really useful!

@pepijn-vanvlaanderen
Copy link

Also at the moment the whole import crashes (returns a 404 on the JSON preview link) when using a resource with any BelongsTo relationships.

@techguydev
Copy link

techguydev commented Apr 28, 2021

@simonhamp thanks a lot for this great package.

I understand that working with relationships is a bit more complex, so far I think that a workaround would be to create a temporary table where to import the data and after it is persistent in the DB we can create a command to handle it as we wish and create models with its relationships.

The best way would be using events, although I've checked and when saving new data to this import table is not firing any saved event, how can it be added?, can it be done through the custom import class you mention in the docs or it would need some extra work?

Thanks

@philsturgeon
Copy link

@simonhamp I'm getting a 404 for my Tree resource as many others are, probably becasue it belongs to Site, Species, and Supervisor. The species should be optional, but the other two are required, and as I'm not given any sort of Nova Action-esque way to prompt the user for those fields they cannot be provided.

The functionality you described in this issue would be amazing, because that import CSV is coming from ArcGIS which has no clue what the IDs are.

Can I throw some money your way to get this working? LMK what sort of sponsorship you need.

@simonhamp
Copy link
Owner Author

@philsturgeon that would be super awesome

What would be ideal from my POV so I can get cracking on this one with less setup is:

  • Your migrations, so I can get the structure of your database
  • Your model and resource classes for those tables
  • A sample of the dataset

In terms of sponsorship, it's entirely up to you - I'd really appreciate some ongoing support (even a small amount) as this would allow me to continue improving the package 🙏

But if you prefer a one-off just to get this feature out, I will leave it up to you to determine the value. I suspect it will take me a couple of days to polish it off and maybe work through some testing with you

@nenads
Copy link

nenads commented Feb 7, 2024

Hi i have some POC.

Idea is that on save dynamically i will get if attribute is relation and if it is i will search it's name and add it.
Any comments would be nice before i do PR. Thanks in advanced.

P.S. maybe this is wrong place ?!

class Importer

public function model(array $row): Model
    {
        $model = $this->resource::newModel();

        $relationshipMethods = collect(
            (new ReflectionClass($model))
                ->getMethods(\ReflectionMethod::IS_PUBLIC)
        )
            ->filter(function (\ReflectionMethod $method) {
                // The `?` makes the code work for methods without an explicit return type.
                return is_subclass_of($method->getReturnType()?->getName(), Relation::class);
            })->values();
        foreach ($relationshipMethods as $key => $relationshipMethod) {

            $relation = $relationshipMethod->getName();

            switch ($relationshipMethod->getReturnType()?->getName()) {
                case BelongsTo::class:
                    $tmp = $model->{$relation}()->make()->where('name', $row[$relation])->firstOrFail();
                    $model->{$relation}()->associate($tmp);
                    unset($row[$relation]); //remove from array since we have added real model
                    break;
                default:
                    # code...
                    break;
            }
        }

        $model->fill($row);

        event("eloquent.creating: $this->model_class", $model); //NOTE:: used for outl1ne/nova-sortable

        return $model;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants