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

Problems with many-to-many relation fields. #90

Closed
Ole-Gi opened this issue Aug 22, 2016 · 5 comments
Closed

Problems with many-to-many relation fields. #90

Ole-Gi opened this issue Aug 22, 2016 · 5 comments

Comments

@Ole-Gi
Copy link

Ole-Gi commented Aug 22, 2016

Hi folks, I have encountered problem while creating/updating entity with many-to-many relation.
I have a field configured like this:

$this->crud->addField([
            'label' => trans('backend/crud_entities.users.field_labels.albums'),
            'type' => 'select2_multiple',
            'name' => 'genres',
            'entity' => 'genres',
            'attribute' => 'name',
            'model' => Genre::class,
            'pivot' => true
        ]);

Class has relation:

public function genres()
    {
        return $this->belongsToMany(Genre::class, 'albums')->withTimestamps();
    }

But when I'm trying to create/update User entity I get the error

ErrorException in helpers.php line 747:
preg_replace(): Parameter mismatch, pattern is a string while replacement is an array
in helpers.php line 747
at HandleExceptions->handleError('2', 'preg_replace(): Parameter mismatch, pattern is a string while replacement is an array', '/home/vagrant/projects/shootster/environment/vendor/laravel/framework/src/Illuminate/Support/helpers.php', '747', array('search' => '?', 'replace' => array('$2y$10$xMtJxgDEjkraJ9bbND5HUucibU0SgDqUsYv8tIuAEm/GrpPnmXotC', '2016-08-22 11:56:18', array('1', '3'), '2'), 'subject' => 'update users set password = y$xMtJxgDEjkraJ9bbND5HUucibU0SgDqUsYv8tIuAEm/GrpPnmXotC, updated_at = 2016-08-22 11:56:18, genres = ? where id = ?', 'value' => array('1', '3')))
at preg_replace('/?/', array('1', '3'), 'update users set password = y$xMtJxgDEjkraJ9bbND5HUucibU0SgDqUsYv8tIuAEm/GrpPnmXotC, updated_at = 2016-08-22 11:56:18, genres = ? where id = ?', '1') in helpers.php line 747

I think a problem is because it tries to save fields for many-to-many relation and falls on the array.
I found solution: to add filtration in trait Create - to unset all relational fields before

public function create($data)
    {
        $values_to_store = $this->compactFakeFields($data, 'create');
        $item = $this->model->create(someFilterFunctionHereToUnsetRelationFileds($values_to_store));

        // if there are any relationships available, also sync those
        $this->syncPivot($item, $data);

        return $item;
    }

Maybe I'm doing something wrong and there is an error in field configuration.
Please help.

Regards, Oleh.

@tabacitu
Copy link
Member

Hi Oleh,

Can you also try declaring the field with:

            'model' => 'App\Models\Genre',

instead of

            'model' => Genre::class,

and tell me if it fixes your issue?

(assuming your Genre model is in a Models folder, otherwise just App\Genre)

Cheers!

@Ole-Gi
Copy link
Author

Ole-Gi commented Aug 22, 2016

Thanks for so fast reaction!
But this fix doesn't help.

@tabacitu
Copy link
Member

Hmm... I just tried it on fresh installation and it works for me, throws no errors. So it's most likely an implementation problem or a very very niche issue.

From the error you gave me it seems like the relationship might not be properly defined. Eloquent tries to save the multiple genre ids in the albums table, which I bet is wrong.

  1. Are you sure your connection table isn't something like album_genre rather than just albums?

  2. How about if you use the default Laravel syntax for the relationship?

public function genres()
    {
        return $this->belongsToMany('App\Models\Genres', 'albums')->withTimestamps();
    }

(most likely not the issue, but worth the shot)

  1. Can you create an album manually, in a different controller (including attaching genres)? Because if that doesn't work, it's definitely the way the relationship has been defined, and not Backpack.

Cheers!

@tabacitu
Copy link
Member

Hi @Ole-Gi ,

Are you still having this issue or have you abandoned?

Thanks, cheers!

@tabacitu
Copy link
Member

P.S. I'll go ahead and close this issue so we don't clog the section. Feel free to open it again if you encounter problems or comment if you have follow-ups.

This was referenced Apr 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants