Stillat / relationships

Provides automated bi-directional @statamic entry relationships
MIT License
15 stars 1 forks source link

Error when working with the database #34

Closed Dragon001-github closed 1 week ago

Dragon001-github commented 3 months ago

Hello, we have encountered a problem while working with a database.

We have created two entities and linked them together as specified in the instructions.

When adding, everything works fine, but when we delete an item from either entity, it remains in the other one.

Looking at the code,we found that when we pull out the old data:

image

The function inside that tries to get the data out of the model tries to find it at the same level when the data in the standard model lies in data.

image image

Is this an actual problem or is this us not handling the model correctly?

We are using the standard driver https://github.com/statamic/eloquent-driver

JohnathonKoster commented 3 months ago

Would you be able to put together a test repo that contains your specific setup that I could look at? That would help to answer your question better. Thanks!

Dragon001-github commented 1 month ago

Hello, I apologize for the long reply.

It's actually quite simple.

I installed pure Statamic, configured the Eloquent driver to store Entry in the database.

2 entities were created through Statamic admin. 2 random fields were merged together. For us it was: Exhibits and Champions.

Communication via Provider was set up.

Relate::manyToMany( 'exhibits.champions', 'champions.exhibits' )->allowDelete(false);

After that when saving everything works fine, when deleting it does not work.

JohnathonKoster commented 1 month ago

When you say deleting does not work, are you saying deleting itself does not work, or that nothing happens with the relationships?

Dragon001-github commented 1 month ago

When you delete a champion from an exhibit, for example, the exhibit remains in the champion.

GertTimmerman commented 1 week ago

We have the same issue. What I have done to test:

Installed a new Statamic and created 2 collections, books and authors with a one to Many relationship. With the flat file system it all works great. I have done exactly the same with de default eloquent driver and then it does not updates the relationship right.

@JohnathonKoster How can I help you to fix this, which info do you need? Because we want to push the new collections to productions, but before that, we want that it works as is should work.

JohnathonKoster commented 1 week ago

@GertTimmerman Thanks for the comment! If you have time, would it be possible to put together a small repo that replicates this using the Eloquent driver and SQLite? This really helps to accelerate my side of things once I get into it

GertTimmerman commented 1 week ago

@GertTimmerman Thanks for the comment! If you have time, would it be possible to put together a small repo that replicates this using the Eloquent driver and SQLite? This really helps to accelerate my side of things once I get into it

I have created it here: https://github.com/GertTimmerman/statamic-entry-relationshop-db

When you change the author of a book, all the relations will mess up

I also think I have the solution, but I not so into the code that i am sure. If you change row /src/Processors/Concerns/GetFieldValue.php:19 to return $entry[$fieldName] ?? $default;

So it look like this:

protected function getPristineValue($fieldName, $entry, $default = null)
    {
        if ($entry instanceof User || (method_exists($entry, 'model') && method_exists($entry, 'toModel'))) {
            $entry = $entry->model();
        }

        if ($entry instanceof Model) {
            return $entry[$fieldName] ?? $default;
        }

        return $this->getFieldValue($fieldName, $entry, $default);
    }

I think that will solve it. And I say think, because the first times it didn't work, and then suddenly it works how it should work

JohnathonKoster commented 1 week ago

Thanks for the repo! I'll take a look this week and get something going for this

JohnathonKoster commented 1 week ago

Improved DB support with 2.2.1