laravel Eloquent:使用另一列作为关系的主键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16551205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:47:04  来源:igfitidea点击:

Eloquent: Use another column as primary key on a relationship

laravellaravel-4eloquent

提问by Marco Rivadeneyra

I have a table Books: with an ID (int) and a ISBN (string) the ISBN is a unique key. I have another table where I store the isbn for the book. On the book model I have: $this->hasMany('Other', 'isbn', 'isbn');

我有一个表 Books: 有一个 ID (int) 和一个 ISBN (string),ISBN 是一个唯一的键。我还有一张桌子,用来存放书的 isbn。在我的书籍模型上: $this->hasMany('Other', 'isbn', 'isbn');

But that ignores the isbn. Do you know any way of doing this?

但这忽略了 isbn。你知道这样做的任何方法吗?

Temporary fix:

临时修复:

$this->primaryKey = 'isbn';
return $this->hasMany('Other', 'isbn');

采纳答案by Phill Sparks

This is not possible - there are only a few instances where you would want to link to a unique column on a table that is not also the pk. That said, other frameworks have developed methods of changing the primary key, and no one has formally requested it for Laravel yet. You can write up a proposal on GitHuband it might make it in to a future release.

这是不可能的 - 只有少数情况下您希望链接到表上的唯一列,而该列也不是 pk。也就是说,其他框架已经开发了更改主键的方法,但还没有人正式要求 Laravel 使用它。你可以在 GitHub 上写一个提案,它可能会加入到未来的版本中。



Looking at your specific example for a moment - this may have unexpected outcomes when you come to use the relationship in eager loading situations, and it only accounts for one side of the relationship. When you add belongsTo on Other you'll have the reverse problem.

看一下您的具体示例 - 当您在急切加载情况下使用关系时,这可能会产生意想不到的结果,并且它仅占关系的一侧。当您在其他上添加belongsTo 时,您将遇到相反的问题。

It may be possible to setup a relationship through isbn; however this would take the form of Book hasOne ISBN, ISBN belongsTo Book, ISBN hasMany Others, Other belongsTo ISBN; and accessing properties would look like $book->isbn->othersor $other->isbn->book.

或许可以通过isbn建立关系;然而,这将采用 Book hasOne ISBN,ISBN 属于 Book,ISBN hasMany Others,Other BeingTo ISBN 的形式;并且访问属性看起来像$book->isbn->othersor $other->isbn->book

My last suggestion would be to extend Book, and set the new class' primary key as isbnand base all your relationships on that model. It's not ideal, but it would be less painful that some other solutions.

我的最后一个建议是扩展 Book,并将新类的主键设置为isbn并将所有关系建立在该模型上。这并不理想,但与其他一些解决方案相比,它不会那么痛苦。

回答by Slava V

Stumbled into the same problem lately, I'm not yet sure whether this would work, but as an idea:

最近偶然发现了同样的问题,我不确定这是否可行,但作为一个想法:

class Book {
    public function isbn() {
        // instead of hasMany
        return ISBN::where('isbn', $this->isbn);
    }
}

回答by Maxime Morin

Since at least 4.2, you can now specify the local key in the hasMany.

至少从 4.2 开始,您现在可以在hasMany.

From the documentation (4.2, 5.2, 5.4, 5.7) :

从文档 ( 4.2, 5.2, 5.4, 5.7) :

return $this->hasMany('Comment', 'foreign_key', 'local_key');

回答by adam

If I'm reading this right, you may be able to achieve this by overriding getKeyName.

如果我没看错,您可以通过覆盖 getKeyName 来实现这一点。

    public function getKeyName(){
        return $yourKeyName;
    }