Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Lumen/Laravel-为什么数据会自动插入连接表?_Php_Mysql_Laravel_Ldap_Lumen - Fatal编程技术网

Php Lumen/Laravel-为什么数据会自动插入连接表?

Php Lumen/Laravel-为什么数据会自动插入连接表?,php,mysql,laravel,ldap,lumen,Php,Mysql,Laravel,Ldap,Lumen,我有三张桌子: 广告用户 Ad_组 广告用户广告组 Ad_usersxad_组是其他两个组的连接表。 现在,我在一个lumen/laravel项目中工作,我们正在使用雄辩的模型。 我有以下ad_用户表和ad_组表的模型: ad_user.php <?php namespace App; use Illuminate\Database\Eloquent\Model; class Ad_user extends Model { /** * The attributes that

我有三张桌子:

广告用户

Ad_组

广告用户广告组

Ad_usersxad_组是其他两个组的连接表。 现在,我在一个lumen/laravel项目中工作,我们正在使用雄辩的模型。 我有以下ad_用户表和ad_组表的模型:

ad_user.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Ad_user extends Model
{
  /**
   * The attributes that are mass assignable.
   *
   * @var array
   */
  protected $fillable = [
    'common_name',
    'location',
    'description',
    'postalcode',
    'physical_delivery_office_name',
    'telephone_number',
    'initials',
    'street_address'
  ];

  /**
   * Hides pivot in return queries.
   *
   * @var array
   */
  protected $hidden = [
    'pivot'
  ];

  /**
   * Many-To-Many relationship.
   */
  public function ad_groups()
  {
    return $this->belongsToMany('App\Ad_group', 'Ad_usersxad_groups');
  }
}
这里,重要的部分是
$ldap->syncData($request->username)。
它调用LDAP.php,执行以下代码:

LDAP.php

  public function syncData($username)
  {
    try
    {
      if(!$this->connect())
      {
        $this->disconnect();
        return false;
      }

      $userData = $this->getUser($username, [
        'cn',
        'l',
        'description',
        'postalcode',
        'physicaldeliveryofficename',
        'telephonenumber',
        'initials',
        'memberof',
        'streetaddress'
      ]);

      $user = Ad_user::updateOrCreate(
        ['common_name' => $userData['cn']],
        [
          'common_name' => $userData['cn'],
          'location' => $userData['l'],
          'description' => $userData['description'],
          'postalcode' => $userData['postalcode'],
          'physical_delivery_office_name' => $userData['physicaldeliveryofficename'],
          'telephone_number' => $userData['telephonenumber'],
          'initials' => $userData['initials'],
          'street_address' => $userData['streetaddress']
        ]
      );

      // Remove everything but the user roles
      foreach($userData['memberof'] as $key=>$role)
      {
        preg_match("/^CN=.+?,/", $role, $role);
        $userData['memberof'][$key] = substr($role[0], 3, -1);
      }

      // Loop through every role because updateOrCreate cant handle arrays
      foreach($userData['memberof'] as $value)
      {
        $roles[] = Ad_group::updateOrCreate(
          ['name' => $value],
          ['name' => $value]
        )->id;
      }

      // Syncs current roles received from ldap with the local db
      $user->ad_groups()->sync($roles);
    }
    catch(\Exception $e)
    {
      $error =  array("exception_code" => $e->getCode(), "error_message" => $e->getMessage());

      Log::error($error);
      return false;
    }
    finally
    {
      $this->disconnect();
    }
  }
我发现,每当用户登录到系统时,记录都会插入到连接表中。 每一次,都意味着创建了大量冗余数据。 我猜导致此问题的代码的重要部分是:

$user->ad_groups()->sync($roles)
例如,同一用户登录7次后,连接表如下所示:

select * from ad_usersxad_groups;
+------------+-------------+------------+------------+
| Ad_user_id | Ad_group_id | created_at | updated_at |
+------------+-------------+------------+------------+
|          1 |           1 | NULL       | NULL       |
|          1 |           2 | NULL       | NULL       |
|          1 |           3 | NULL       | NULL       |
|          1 |           4 | NULL       | NULL       |
|          1 |           5 | NULL       | NULL       |
|          1 |           6 | NULL       | NULL       |
|          1 |           7 | NULL       | NULL       |
|          1 |           8 | NULL       | NULL       |
|          1 |           9 | NULL       | NULL       |
|          1 |          10 | NULL       | NULL       |
|          1 |          11 | NULL       | NULL       |
|          1 |          12 | NULL       | NULL       |
|          1 |           1 | NULL       | NULL       |
|          1 |           2 | NULL       | NULL       |
|          1 |           3 | NULL       | NULL       |
|          1 |           4 | NULL       | NULL       |
|          1 |           5 | NULL       | NULL       |
|          1 |           6 | NULL       | NULL       |
|          1 |           7 | NULL       | NULL       |
|          1 |           8 | NULL       | NULL       |
|          1 |           9 | NULL       | NULL       |
|          1 |          10 | NULL       | NULL       |
|          1 |          11 | NULL       | NULL       |
|          1 |          12 | NULL       | NULL       |
|          1 |           1 | NULL       | NULL       |
|          1 |           2 | NULL       | NULL       |
|          1 |           3 | NULL       | NULL       |
|          1 |           4 | NULL       | NULL       |
|          1 |           5 | NULL       | NULL       |
|          1 |           6 | NULL       | NULL       |
|          1 |           7 | NULL       | NULL       |
|          1 |           8 | NULL       | NULL       |
|          1 |           9 | NULL       | NULL       |
|          1 |          10 | NULL       | NULL       |
|          1 |          11 | NULL       | NULL       |
|          1 |          12 | NULL       | NULL       |
|          1 |           1 | NULL       | NULL       |
|          1 |           2 | NULL       | NULL       |
|          1 |           3 | NULL       | NULL       |
|          1 |           4 | NULL       | NULL       |
|          1 |           5 | NULL       | NULL       |
|          1 |           6 | NULL       | NULL       |
|          1 |           7 | NULL       | NULL       |
|          1 |           8 | NULL       | NULL       |
|          1 |           9 | NULL       | NULL       |
|          1 |          10 | NULL       | NULL       |
|          1 |          11 | NULL       | NULL       |
|          1 |          12 | NULL       | NULL       |
|          1 |           1 | NULL       | NULL       |
|          1 |           2 | NULL       | NULL       |
|          1 |           3 | NULL       | NULL       |
|          1 |           4 | NULL       | NULL       |
|          1 |           5 | NULL       | NULL       |
|          1 |           6 | NULL       | NULL       |
|          1 |           7 | NULL       | NULL       |
|          1 |           8 | NULL       | NULL       |
|          1 |           9 | NULL       | NULL       |
|          1 |          10 | NULL       | NULL       |
|          1 |          11 | NULL       | NULL       |
|          1 |          12 | NULL       | NULL       |
|          1 |           1 | NULL       | NULL       |
|          1 |           2 | NULL       | NULL       |
|          1 |           3 | NULL       | NULL       |
|          1 |           4 | NULL       | NULL       |
|          1 |           5 | NULL       | NULL       |
|          1 |           6 | NULL       | NULL       |
|          1 |           7 | NULL       | NULL       |
|          1 |           8 | NULL       | NULL       |
|          1 |           9 | NULL       | NULL       |
|          1 |          10 | NULL       | NULL       |
|          1 |          11 | NULL       | NULL       |
|          1 |          12 | NULL       | NULL       |
|          1 |           1 | NULL       | NULL       |
|          1 |           2 | NULL       | NULL       |
|          1 |           3 | NULL       | NULL       |
|          1 |           4 | NULL       | NULL       |
|          1 |           5 | NULL       | NULL       |
|          1 |           6 | NULL       | NULL       |
|          1 |           7 | NULL       | NULL       |
|          1 |           8 | NULL       | NULL       |
|          1 |           9 | NULL       | NULL       |
|          1 |          10 | NULL       | NULL       |
|          1 |          11 | NULL       | NULL       |
|          1 |          12 | NULL       | NULL       |
+------------+-------------+------------+------------+
这既不是期望的,也不是可以接受的,但是因为我对lumen/laravel以及这里使用的php LDAP API都是非常陌生的,所以我几乎不知道是什么导致了这种行为,至少我非常确定MySQL/MariaDB不会影响这种行为,因为外键没有连接到它们的级联

我想知道我是否在模型的定义上做错了什么,但我不知道xD是什么 如果您能提供一些帮助,我们将不胜感激^^

编辑: 日志显示了角色阵列的以下内容:

[2020-01-31 08:57:27] local.INFO: array (
  0 => 1,
  1 => 2,
  2 => 3,
  3 => 4,
  4 => 5,
  5 => 6,
  6 => 7,
  7 => 8,
  8 => 9,
  9 => 10,
  10 => 11,
  11 => 12,
)  
仅供参考,这里是数据库的外键,当然包括与此问题相关的外键。我很确定我正确地配置了它们,但也许我只是瞎了,所以它们在这里:

| TABLE_NAME                         | COLUMN_NAME                 | CONSTRAINT_NAME                      | REFERENCED_TABLE_NAME    | REFERENCED_COLUMN_NAME |
+------------------------------------+-----------------------------+--------------------------------------+--------------------------+------------------------+
| ad_usersxad_groups                 | Ad_user_id                  | fk_ad_groupxad_user                  | ad_users                 | id                     |
| ad_usersxad_groups                 | Ad_group_id                 | fk_ad_userxad_group                  | ad_groups                | id                     |
| extensiontables_registryxad_groups | ad_group_id                 | fk_ad_groupxextensiontables_registry | ad_groups                | id                     |
| extensiontables_registryxad_groups | extensiontables_registry_id | fk_extensiontables_registryxad_group | extensiontables_registry | id                     |
| extensiontable_itc                 | coretable_id                | fk_extensiontable_itc_coretable      | coretable                | id                     |
| inaccessibletable                  | coretable_id                | fk_inaccessibletable_coretable       | coretable                | id                     |
+------------------------------------+-----------------------------+--------------------------------------+--------------------------+------------------------+

从我看到的情况来看,转储的数组和代码看起来都不错

我不是拉威尔的专家,但我建议在定义你们的关系时要更加具体,看看这是否能解决你们的问题

在您的
Ad\u用户
型号中,更改:

public function ad_groups()
{
    return $this->belongsToMany('App\Ad_group', 'Ad_usersxad_groups');
}
public function ad_users()
{
    return $this->belongsToMany('App\Ad_user', 'Ad_usersxad_groups');
}
致:

在您的
Ad_组中
model,更改:

public function ad_groups()
{
    return $this->belongsToMany('App\Ad_group', 'Ad_usersxad_groups');
}
public function ad_users()
{
    return $this->belongsToMany('App\Ad_user', 'Ad_usersxad_groups');
}
致:


如果这不能解决您的问题,请转储
$roles
数组并将其发布到此处。虽然没有解决问题,但仍然感谢您的建议!:D我会立即转储$roles数组,只要给我一秒就行了,值得一试,是的,请这样做。我将删除或更新我的答案。在这里转储日志,我必须承认我现在对更新的答案感到非常困惑,不确定这是否会有帮助,但还是值得一试。
public function ad_users()
{
    return $this->belongsToMany('App\Ad_user', 'Ad_usersxad_groups');
}
public function ad_users()
{
    return $this->belongsToMany('App\Ad_user', 'Ad_usersxad_groups', 'Ad_group_id', 'Ad_user_id');
}