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

edit_fields in type "relationship" doesn't work when foreign key is a non-primary key. #1048

Open
SeptimusLiu opened this issue Sep 24, 2016 · 0 comments

Comments

@SeptimusLiu
Copy link

Hi, it seems that when I use the type "relationship" in edit_fields in model config file, it strangely update the id fields instead of the key group I've set in $this->belongsTo('\App\Models\MtrGroup', 'group', 'group');. What confused me is that it just outputs the right result in columns, but doesn't work with edit_fields and filters. Table schema, eloquent model and model config is as follows, hope you can help me solve it, thx.

Table schema:

  • mtr_groups
CREATE TABLE `mtr_groups` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `order` int(11) NOT NULL DEFAULT '0',
  `group` varchar(20) NOT NULL DEFAULT '',
  `name` varchar(50) NOT NULL DEFAULT '',
  `active` enum('Y','N') NOT NULL DEFAULT 'N',
  `created_time` datetime NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `order` (`order`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4;
  • mtr_mid_category
CREATE TABLE `mtr_mid_categories` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `order` int(11) NOT NULL DEFAULT '0',
  `group` varchar(20) NOT NULL DEFAULT '',
  `name` varchar(50) NOT NULL DEFAULT '',
  `created_time` datetime NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `order` (`order`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4;

Eloquent model:

  • MtrGroup.php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class MtrGroup extends Model
{
    protected $table = 'mtr_groups';
    public $timestamps = false;
    protected $hidden = ['created_time'];
}
  • MtrMidCategory.php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class MtrMidCategory extends Model
{
    protected $table = 'mtr_mid_categories';
    public $timestamps = false;
    protected $hidden = ['order', 'created_time'];

    public function mtr_group()
    {
        return $this->belongsTo('\App\Models\MtrGroup', 'group', 'group');
    }
}

Model config file:

  • mtr_mid_category.php
<?php

return [
    'title' => 'Middle Category',

    'single' => 'Middle Category',

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

    'columns' => [
        'name' => [
            'title' => 'Name',
        ],
        'group_name' => [
            'title' => 'Group',
            'relationship' => 'mtr_group',
            'select' => "(:table).name",
        ],
        'order' => [
            'title' => 'Order',
        ],
    ],

    'edit_fields' => [
        'name' => [
            'title' => 'Name',
            'type' => 'text',
        ],
        'mtr_group' => [
            'title' => 'Group',
            'type' => 'relationship',
            'name_field' => 'name',
        ],
        'order' => [
            'title' => 'Order',
            'type' => 'number',
        ],
    ],

    'filters' => [
        'name' => [
            'title' => 'Name',
        ],
        'mtr_group' => [
            'title' => 'Group',
            'type' => 'relationship',
            'name_field' => 'name',
        ],
    ],
];

Many mtr_mid_category belong to one group, and related by the key group (not id, it turns to be correct when I use id as foreign key), so why did edit and filter just update and select id? Is there any wrong here?

@SeptimusLiu SeptimusLiu changed the title edit_fields in type "relationship" doesn't work when belongsTo() referred to a non-primary key. edit_fields in type "relationship" doesn't work when foreign key is a non-primary key. Sep 24, 2016
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

1 participant