多对多的自定义外键

时间:2014-05-15 07:53:21

标签: sql laravel laravel-4 many-to-many eloquent

我有三张桌子。像这样

mysql> desc users;
+----------------+------------------+------+-----+---------------------+----------------+
| Field          | Type             | Null | Key | Default             | Extra          |
+----------------+------------------+------+-----+---------------------+----------------+
| id             | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| email          | varchar(100)     | NO   | UNI | NULL                |                |
| password       | varchar(64)      | NO   |     | NULL                |                |
| remember_token | varchar(100)     | YES  |     | NULL                |                |
| created_at     | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
| updated_at     | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
+----------------+------------------+------+-----+---------------------+----------------+
6 rows in set (0.00 sec)

mysql> desc languages;
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| id          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| code        | varchar(2)       | NO   | MUL | NULL    |                |
| name        | varchar(100)     | NO   |     | NULL    |                |
| native_name | varchar(100)     | NO   |     | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

mysql> desc user_know_languages;
+---------------+------------------+------+-----+---------------------+----------------+
| Field         | Type             | Null | Key | Default             | Extra          |
+---------------+------------------+------+-----+---------------------+----------------+
| id            | int(10) unsigned | NO   | PRI | NULL                | auto_increment |
| user_id       | int(10) unsigned | NO   | MUL | NULL                |                |
| language_code | varchar(2)       | NO   | MUL | NULL                |                |
| read_id       | int(10) unsigned | NO   |     | 1                   |                |
| write_id      | int(10) unsigned | NO   |     | 1                   |                |
| speak_id      | int(10) unsigned | NO   |     | 1                   |                |
| created_at    | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
| updated_at    | timestamp        | NO   |     | 0000-00-00 00:00:00 |                |
+---------------+------------------+------+-----+---------------------+----------------+
8 rows in set (0.00 sec)

我的用户模型

class User{
    public function knowLanguages()
    {
        return $this->belongsToMany('Language', 'user_know_languages', 'user_id', 'language_code');
    }
}

就像这个sql生成的那样

select `languages`.*, `user_know_languages`.`user_id` as `pivot_user_id`, `user_know_languages`.`language_code` as `pivot_language_code` from `languages` inner join `user_know_languages` on `languages`.`id` = `user_know_languages`.`language_code` where `user_know_languages`.`user_id` = ?

我想....在languages.code = user_know_languages.language_code ...而不是languages.id = user_know_languages.language_code

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

像这样定义主键:

// Language model
protected $primaryKey = 'code';