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

When saving an entity containing a HasOne relationship, Voyager tries to write the related id into the wrong table. #1866

Open
artifacts opened this Issue Sep 25, 2017 · 2 comments

Comments

Projects
None yet
4 participants
@artifacts

artifacts commented Sep 25, 2017

  • Laravel Version: 5.5.*
  • Voyager Version: 1.0.0
  • PHP Version: 7.1
  • Database Driver & Version: mysql, utf8mb4, utf8mb4_unicode_ci

Description:

The entity cannot be saved since voyager is complaining about a missing field. That field is supposed to be looked up in the db table of the related, not the source object.

Example:

An Event has one Agenda. The agenda has a db column named event_id.

The Model classes are as follows:

App\Event.php

class Event extends Model
{
  public function agenda()
  {
    return $this->hasOne(Agenda::class);
  }
}

App\Agenda.php

class Agenda extends Model
{

  public function event()
  {
    return $this->belongsTo(Event::class);
  }

}

DB table for agendas:

screen shot 2017-09-25 at 22 32 10

DB table for events:
screen shot 2017-09-25 at 22 32 21

The BREAD config of Event looks like this:

screen shot 2017-09-25 at 21 57 53

Problem 1:
No dropdown is rendered (see screenshot)

screen shot 2017-09-25 at 22 26 17

Problem 2:
When trying to create a new event, an error is thrown:

"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'event_id' in 'field list' (SQL: insert into events (title, event_id, updated_at, created_at) v ▶"

Problem 3:
The text describing the relation is wrong:

screen shot 2017-09-25 at 21 57 53

Steps To Reproduce:

Extract and run the attached project. The DB dump is inside the app's root folder. It is populated with three Agenda Entities. Go to Events in the Menu on the left and try to create a new one.

Expected result: The Form renders a dropdown, allowing the user to select an Agenda.

Actual result: "Agenda 1" - the name of the first Agenda Entity is rendered as plain text instead of a dropdown. When trying to save the Event an Exception is thrown.

screen shot 2017-09-25 at 22 27 13

Test Project:

voyager-relations-test.zip

@alonweinstein

This comment has been minimized.

Show comment
Hide comment
@alonweinstein

alonweinstein Oct 3, 2017

vendor/tcg/voyager/src/Http/Controllers/Controller.php:

change:

if ($row->type == 'relationship') { $row->field = @$options->column; }

to:
if ($row->type == 'relationship') { if ($options->type == 'hasMany') { break; } else { $row->field = @$options->column; } }

Skips hasMany relationship fields. Not sure if there's a use-case where some update actually needs to happen on the related rows.

alonweinstein commented Oct 3, 2017

vendor/tcg/voyager/src/Http/Controllers/Controller.php:

change:

if ($row->type == 'relationship') { $row->field = @$options->column; }

to:
if ($row->type == 'relationship') { if ($options->type == 'hasMany') { break; } else { $row->field = @$options->column; } }

Skips hasMany relationship fields. Not sure if there's a use-case where some update actually needs to happen on the related rows.

@StevenKTurnerLuna

This comment has been minimized.

Show comment
Hide comment
@StevenKTurnerLuna

StevenKTurnerLuna Oct 10, 2017

Running into the same issue, currently setting up these relationships without Edit, Add and Delete options, which avoids the issue.

StevenKTurnerLuna commented Oct 10, 2017

Running into the same issue, currently setting up these relationships without Edit, Add and Delete options, which avoids the issue.

osi8 added a commit to osi8/voyager that referenced this issue Oct 23, 2017

FIX the-control-group#1866 hasMany relationships include wrong column
HasMany Relationships fail on update and insert, because the related id column is included.
Example: Post hasMany Comments: trying to update one post includes "... `post_id`=  WHERE ..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment