属于一个表与其本身之间的许多关系 [英] BelongstoMany relationship between a table and itself

查看:57
本文介绍了属于一个表与其本身之间的许多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有表Users的数据库,我需要存储有关它们之间的一种关系的信息:有权管理其他用户的用户.另外,那些受管理的用户也可以是管理员,因此任何用户都可以是管理员,也可以由包括自己在内的任何其他用户来管理(两个用户可以是彼此的管理员).

I have a database with a table Users and i need to store information about one relationship between them: users that have permissions to manage other users. Also those managed users can be managers too, so any user can be manager or be managed by any other user including themselves (2 users can be managers of each other).

所以我唯一想到的就是用这些字段创建一个"join"表:

So the only idea coming to my mind is to create a "join" table with these fields:

manager_id (key field that links to a user_id)
managed_id (key field too that links to a user_id as well)

但是在同一个表之间的联接表,将产生哪种关系?

But a join table between the same table, which kind of relationship will produce?

Users-> belongsToMany('Users')??

Users->belongsToMany('Users') ??

这样可以吗?

使用ndm告诉我的方法时出现错误.

I'm getting an error using the approach ndm told me.

因此,在UsersTable.php中,我现在拥有:

So, in UsersTable.php i have now:

$this->belongsToMany('Managers', [
            'className' => 'Users',
            'foreignKey' => 'manager_id',
            'targetForeignKey' => 'user_id'
         ]);

在数据库"test.sql"表中,managers_users具有2个字段:

In database "test.sql" table managers_users has 2 fields:

manager_id
user_id

两个关键字段

但是当我尝试从UsersController.php查询时:

But when i try to query from UsersController.php:

$query = $this->Users->find()
                ->contain(['Managers'])
                ->all()
                ;

我得到错误:SQLSTATE [42S02]:未找到基表或视图:1146表'test.users_users'不存在

I get error: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.users_users' doesn't exist

有帮助吗?

推荐答案

联接表通常包含一个 belongsToMany 关联,该关联基本上是 hasMany belongsTo ,也可以手动表示,也可以使用 hasOne belongsTo 表示,这实际上取决于您的需求应用程序,因此您可能应该首先评估应用程序在什么情况下需要如何查询数据,然后再从那里开始.

A join table usually involves a belongsToMany association, which is basically a combination of hasMany and belongsTo under the hood, and could also manually expressed as such, it could also be expressed using hasOne and belongsTo, it really depends on the needs of your application, so you probably should first asses how your application needs to query the data in what situation, and then go from there.

通常,自我关联很好(请参阅Cookbook关联章节中的介绍),所以是的,在主原则上是可以的,但是您需要设置/配置一些稍有不同的东西才能使它起作用正确地,那就是您需要使用唯一的别名,例如 Managers 而不是 Users ,然后您的外键字段都需要适应约定,即 user_id (受管理用户)和 manager_id (管理用户),或者您必须在关联配置中进行相应的配置:

Generally self-associations are fine (see the introduction in the Cookbook association chapter),, so yes, in pricipal that would be okay, however you'd need to setup/configure things a little different in order for this to work properly, that is that you'd need to use a unique alias, for example Managers instead of Users, and then either your foreign key fields need to be adapted to the conventions, ie user_id (managed user) and manager_id (managing user), or you would have to configure them accordingly in the association configuration:

$this->belongsToMany('Managers', [
    'className' => 'Users',

    //'foreignKey' => 'managed_id',
    //'targetForeignKey' => 'manager_id'
]);

另请参见

这篇关于属于一个表与其本身之间的许多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆