Laravel Eloquent复合主键关系

时间:2015-09-08 14:08:38

标签: laravel foreign-keys eloquent relationship

我有以下架构:

Schema::create('companies', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name')->unique();
    $table->timestamps();
});

Schema::create('departments', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('company_id');
    $table->string('name');
    $table->timestamps();

    $table->foreign('company_id')->references('id')->on('companies');
    $table->unique(['company_id','name']);
}); 

Schema::create('employees', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('company_id');
    $table->string('name');
    $table->timestamps();

     $table->foreign('company_id')->references('id')->on('companies');
 });


Schema::create('managed_departments', function (Blueprint $table) {
    $table->integer('company_id');
    $table->integer('department_id');
    $table->integer('manager_id');
    $table->timestamps();

    $table->primary(['company_id','department_id']);


    // Overlapping foreign keys guarantee that the manager 
    // and the department belong to the same company.

    $table->foreign(['company_id','department_id'])
          ->references(['company_id','department_id'])
          ->on('departments');

    $table->foreign(['company_id','manager_id'])
          ->references(['company_id','id'])
          ->on('employees');
 });

 Schema::create('department_staff', function (Blueprint $table) {
    $table->integer('company_id');
    $table->integer('department_id');
    $table->integer('employee_id');
    $table->timestamps();

    $table->primary(['company_id','department_id', 'employee_id']);


    // Overlapping foreign keys guarantees that the employee and the 
    // managed department belong to the same company.

    $table->foreign(['company_id','department_id'])
          ->references(['company_id','department_id'])
          ->on('managed_departments');

    $table->foreign(['company_id','employee_id'])
          ->references(['company_id','id'])
          ->on('employees');
 });

什么是部门,managed_departments和department_staff表之间的正确模型关系 上面的模式 - 当有复合主键时,我对如何定义关系感到有点困惑,以下内容对我来说不正确,以及如何雄辩地知道正确的外键?

class Department extends Model
{
   // a department is managed by an employee
   public function managedDepartment
   {
       $this->hasOne(app\ManagedDepartment)
   }
}

class ManagedDepartment extends Model
{
   // a managed department belongs to department
   public function Department
   {
      $this->belongsTo(app\Department)
   }

   // a managed department is managed by an employee
   public function Employee
   {
      $this->belongsTo(app\Employee)

   }
}

class DepartmentStaff extends Model
{
   public function employee()
   {
       return $this->belongsTo('App\Employee');
   }
}

class Employee extends Model
{
   // an employee can managed many depts
   public function managedDepartment
   {
      $this->hasMany(app\ManagedDepartment)
   }

   // an employee is asigned to one department
   public function departmentStaff()
   {
       return $this->hasOne('App\DepartmentStaff');
   }
}

0 个答案:

没有答案