简体   繁体   English

cakePHP 3中的自链接模型,并从连接表中检索数据

[英]Self linking model in cakePHP 3 and retrieving data from the join table

im creating a WebApp with cakePHP 3.0. 我用cakePHP 3.0创建一个WebApp。

I have a User Model and Table which has and belongs to many Users. 我有一个用户模型和表,它拥有并属于许多用户。

So i set up tables according to cake conventions: 所以我根据蛋糕惯例设置表格:

CREATE TABLE IF NOT EXISTS `users`
(
user_id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
);

and the JOIN table: 和JOIN表:

CREATE TABLE IF NOT EXISTS `users_users`
(
supervisor_id int,
employee_id int,
FOREIGN KEY (supervisor_id) REFERENCES users(user_id),
FOREIGN KEY (employee_id) REFERENCES users(user_id)
);

In my UserTable.php i created these associations: 在我的UserTable.php中,我创建了这些关联:

    $this->table('users');
    $this->primaryKey('user_id');

    $this->belongsTo('Users', [
                'foreignKey' => 'user_id',
                'joinType' => 'INNER'
   ]);

    $this->belongsToMany('Supervisors', [
                'className' => 'Users',
                'targetForeignKey' => 'supervisor_id'
    ]);
    $this->belongsToMany('Employees', [
                'className' => 'Users',
                'targetForeignKey' => 'employee_id'
    ]);

Now in a new model method i want to get the Supervisors of one employee: 现在,在一个新的模型方法中,我希望得到一名员工的主管:

public function getSupervisorEmail($user_id) {
        $supervisors = $this->Employees->Supervisors->find()->select(['user_id']);
        debug($supervisors);
        return $supervisors;
    }

I put sample data in my users_users Table but i dont know how i can access these entries. 我将示例数据放在我的users_users表中,但我不知道如何访问这些条目。 The query in the function above doesnt do what i expect it to do. 上面函数中的查询没有做我期望它做的事情。 It just returns a record from my Users Table without joining with users_users Table what i dont understand because i set all up according to naming conventions of cakePHP 3... 它只是从我的用户表中返回一条记录而没有加入users_users表我不明白因为我根据cakePHP 3的命名约定设置了所有...

How can i access my join Table and get associated records? 如何访问我的联接表并获取相关记录? EG get Supervisors associated to user with user_id 1. I tried different querys but none used my join table. EG通过user_id 1获取与用户关联的主管。我尝试了不同的查询但没有使用我的连接表。

Thanks for answers! 谢谢你的回答!

Specify the join table object in the through option of the HABTM (In 3.0 it has been renamed to belongsToMany / BTM) assoc. 在HABTM的through选项中指定连接表对象(在3.0中,它已重命名为belongsToMany / BTM)assoc。 Otherwise it will inflect and construct a Table object instance based on the table name. 否则,它将根据表名反射并构造一个Table对象实例。 I would always create the join table object, so just bake it for now. 总是会创建连接表对象,所以现在就把它烘烤一下。

http://book.cakephp.org/3.0/en/orm/associations.html#belongstomany-associations http://book.cakephp.org/3.0/en/orm/associations.html#belongstomany-associations

through Allows you to provide a either the name of the Table instance you want used on the join table, or the instance itself. through允许您提供要在连接表上使用的Table实例的名称,或者提供实例本身。 This makes customizing the join table keys possible, and allows you to customize the behavior of the pivot table. 这使得可以自定义连接表键,并允许您自定义数据透视表的行为。

You're not querying additional assocs. 你不是在查询其他的关联。 contain() your join table model in your query. 在查询中包含()您的连接表模型。

http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#eager-loading-associations http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#eager-loading-associations

By default CakePHP does not load any associated data when using find() . 默认情况下,使用find()时,CakePHP不会加载任何关联的数据 You need to 'contain' or eager-load each association you want loaded in your results. 您需要“包含”或急切加载要在结果中加载的每个关联。

$query = $articles->find('all');
$query->contain(['Authors', 'Comments']);

Problem was that I got the concept wrong. 问题是我的概念错了。 The SQL statement when you debug the query doesnt show every SQL stuff which happens internally in cakePHP so u dont see the automatic joins. 调试查询时的SQL语句不会显示cakePHP内部发生的每个SQL内容,因此您不会看到自动连接。

But now i figured out that it works perfectly by echoing testdata like in the cakePHP book http://book.cakephp.org/3.0/en/orm/associations.html#belongstomany-associations 但现在我发现它完美地通过回应testdata就像在cakePHP书中http://book.cakephp.org/3.0/en/orm/associations.html#belongstomany-associations

Thanks though @burzum for trying to help me the provided links are always helpfull. 谢谢@burzum试图帮助我提供的链接总是有帮助的。

$users = $this->Users->find('all', [
            'order' => [],            
            'conditions' => ['Users.id' => 1],
            'contain' => ['Supervisors','Employees']
                ]
        );

use this 用这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM