Cake:habtm 与其他模型上的非默认键的关系

发布于 2024-12-05 17:33:03 字数 1218 浏览 0 评论 0原文

我有 2 个需要通过 habtm 关系链接的模型,具有以下表结构:

CATEGORIES:

id   |   name   | ..
-----------------------
 1   |   test   | ..

POSTS:

id   |   name   | other_id | ..
---------------------------------
 1   |   test   | 5        | ..

CATEGORIES_POSTS:

id   |   category_id | other_id
--------------------------------
 1   |   1           |  5

我需要从类别端获取帖子,但似乎无法设置 habtm 关系正确。到目前为止我还没有提到的重要一点是,Post-model 中使用的 id 不是 id 而是 other_id。这是我到目前为止所尝试的(全部在类别模型中):

  • 将 AssociationForeignKey 设置为“other_id”

    在sql查询中它有:CategoriesPost.other_id = Post.id片段->错误的关系(应该是CategoriesPost.other_id = Post.other_id

  • 将 AssociationForeignKey 设置为 false 并添加条件CategoriesPost.other_id = Post.other_id

    现在sql片段是CategoriesPost。 = Post.id --> sql错误

  • 将associationForeignKey设置为CategoriesPost.other_id = Post.other_id

    嗯..这也是一个错误,因为Cake将输入作为1个字段:CategoriesPost.other_id = Post.other_id = Post.id

我知道我可以通过 2 个 hasMany 链接实现关系,但这给了我很多查询,而不是 1 个

提前致谢!

I have 2 models that need to be linked by a habtm relationship, having this table-structure:

CATEGORIES:

id   |   name   | ..
-----------------------
 1   |   test   | ..

POSTS:

id   |   name   | other_id | ..
---------------------------------
 1   |   test   | 5        | ..

CATEGORIES_POSTS:

id   |   category_id | other_id
--------------------------------
 1   |   1           |  5

I need to get the posts from the category side, but don't seem to be able to set the habtm relation correctly. The important thing, that I didn't mention so far, is that the id used in the Post-model is not id but other_id. This is what I tried so far (all in the Category-model):

  • set the associationForeignKey to 'other_id'

    in the sql-query it has: CategoriesPost.other_id = Post.id fragment -> wrong relation (should be CategoriesPost.other_id = Post.other_id

  • set the associationForeignKey to false and add a condition CategoriesPost.other_id = Post.other_id

    now the sql fragment is CategoriesPost. = Post.id --> sql error

  • set the associationForeignKey to CategoriesPost.other_id = Post.other_id

    well .. this is an error as well, as Cake takes the input as 1 field: CategoriesPost.other_id = Post.other_id = Post.id

I know I could achieve to relation through 2 hasMany links, but that gives me a lot of queries instead of 1

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

血之狂魔 2024-12-12 17:33:03

只需动态更改后模型的primaryKey 来进行一些您需要的操作......
为此,只需执行 $this->primaryKey = 'other_id' 或在控制器中 $this->Post->primaryKey= 'other_id'

即可。

但请记住,如果您要从所有关联中检索数据,并且您有比这个关联更多的关联,那么其他关联如果使用 Post.id 将失败,因为主键是 Post.other_id

您应该在帖子模型中执行查找功能当你使用这个联合时,类似这样的事情:

function otherFind ($type, $options){
    $this->primaryKey = 'other_id';
    $result = $this->find($type, $options);
    $this->primaryKey = 'id';
    return $result;
}

如果你需要将它与其他模型连接起来会变得有点棘手,我会建议使用连接(尝试查看可链接的行为代码以了解如何操作)

我强烈建议只使用一个主键,因为第二个键并没有多大帮助。无论如何,主键应该是唯一的,并且您可以将任何内容与仅一个主键相关联。

just change the post model primaryKey on the fly for some operations you need to....
To do so, just need to do $this->primaryKey = 'other_id' OR in a controller $this->Post->primaryKey= 'other_id'

that would do the trick.

But remember, if you are retrieving data from all associations and you have more associations than this one then the other associations if they use Post.id are going to fail since primary key is Post.other_id

you should do a find function in your post models for when you are using this union, something like this:

function otherFind ($type, $options){
    $this->primaryKey = 'other_id';
    $result = $this->find($type, $options);
    $this->primaryKey = 'id';
    return $result;
}

if you need to join it with other models gets a little more tricky i will recommend to use joins for that (try looking at the linkable bhaviour code to see how)

I strongly suggest to use only ONE primary key since it's not really helpfull a second one. A primary key should be unique anyway and you can associate anything with just one.

錯遇了你 2024-12-12 17:33:03

在进行正常查找时,Cake 无法自定义在连接上使用的主键。

如果您确实需要,您可以使用自定义联接: http://book.cakephp.org /view/1047/Joining-tables

为什么需要两个 id?您正在尝试将帖子加入类别,无论如何 ID 都是唯一的;就两者的关系而言,主要的应该工作得很好。

Cake can't customise the primary key to use on the join when doing a normal find.

You could use a custom join, if you really want: http://book.cakephp.org/view/1047/Joining-tables

Why exactly do you need two ids? You are trying to join a post to a category, the ids will be unique anyway; as far as relating the two, the primary should work just fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文