Laravel 5.3 + MongoDB 库 'jenssegers/laravel-mongodb' 中有许多关系问题
hasMany relationship issue in Laravel 5.3 + MongoDB library 'jenssegers/laravel-mongodb'
我正在 Laravel 5.3
中使用 MongoDB
库。我在 MongoDB
中有两个集合,我想与它们建立 hasMany
关系 b/w。
MongoDB:
第一个合集:
员工
{
"_id" : ObjectId("586ca8c71a72cb07a681566d"),
"employee_name" : "John",
"employee_description" : "test description",
"employee_email" : "john@email.com",
"updated_at" : "2017-01-04 11:45:20",
"created_at" : "2017-01-04 11:45:20"
},
{
"_id" : ObjectId("586ca8d31a72cb07a6815671"),
"employee_name" : "Carlos",
"employee_description" : "test description",
"employee_email" : "carlos@email.com",
"updated_at" : "2017-01-04 11:45:20",
"created_at" : "2017-01-04 11:45:20"
}
第2集:
任务
{
"_id" : ObjectId("586ccbcf1a72cb07a6815b04"),
"task_name" : "New Task",
"task_description" : "test description",
"task_status" : 1,
"task_created_at" : "2017-04-01 02:17:00",
"task_updated_at" : "2017-04-01 02:17:00"
},
{
"_id" : ObjectId("586cd3261a72cb07a6815c69"),
"task_name" : "2nd Task",
"task_description" : "test description",
"task_status" : 1,
"task_created_at" : "2017-04-01 02:17:00",
"task_updated_at" : "2017-04-01 02:17:00"
}
我在它们之间创建了枢轴table
Employee_Task
{
"_id" : ObjectId("586cd0cb1a72cb07a6815bf3"),
"employee_task_employee_id" : "586ca8c71a72cb07a681566d",
"employee_task_task_id" : "586ccbcf1a72cb07a6815b04",
"status" : 1
},
{
"_id" : ObjectId("586cd7851a72cb07a6815d7d"),
"employee_task_employee_id" : "586ca8c71a72cb07a681566d",
"employee_task_task_id" : "586cd3261a72cb07a6815c69",
"status" : 1
}
Laravel:
型号:
员工:
<?php
namespace App\Models;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class Employee extends Eloquent {
protected $collection = 'employee';
protected $primaryKey = '_id';
public function tasks()
{
return $this->hasMany('App\Models\Task');
}
}
任务:
<?php
namespace App\Models;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class Task extends Eloquent {
protected $collection = 'task';
protected $primaryKey = '_id';
public function employees()
{
return $this->belongsToMany('App\Models\Employee');
}
}
控制器:
public function EmployeeData($data)
{
$employee= Employee::find('586ca8c71a72cb07a681566d')->tasks;
echo "<pre>";
print_r($employee);
}
当我想查看针对员工的任务时,它会显示以下输出:
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
)
)
我该如何解决这个问题?
在 Mongo Eloquent 中创建多对多关系时你不需要有一个枢轴 table,那是 SQL 心态,在 mongo- eloquent 外键存储在数组中的多对多关系。
所以模型应该是这样的:
<?php namespace App\Models;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class Employee extends Eloquent {
protected $collection = 'employee';
protected $primaryKey = '_id';
public function tasks()
{
return $this->belongsToMany('App\Models\Task');
}
}
<?php namespace App\Models;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class Task extends Eloquent {
protected $collection = 'task';
protected $primaryKey = '_id';
public function employees()
{
return $this->belongsToMany('App\Models\Employee');
}
}
此外,您应该在尝试检索关系之前先加载它们
$employee= Employee::with('tasks')->find('586ca8c71a72cb07a681566d')->tasks;
您可以像在 hasMany 关系中那样保存关系
$employee->tasks()->save(new Task());
是的,在这种情况下您需要使用 belongsToMany 而不是 hasMany...
我正在 Laravel 5.3
中使用 MongoDB
库。我在 MongoDB
中有两个集合,我想与它们建立 hasMany
关系 b/w。
MongoDB:
第一个合集: 员工
{
"_id" : ObjectId("586ca8c71a72cb07a681566d"),
"employee_name" : "John",
"employee_description" : "test description",
"employee_email" : "john@email.com",
"updated_at" : "2017-01-04 11:45:20",
"created_at" : "2017-01-04 11:45:20"
},
{
"_id" : ObjectId("586ca8d31a72cb07a6815671"),
"employee_name" : "Carlos",
"employee_description" : "test description",
"employee_email" : "carlos@email.com",
"updated_at" : "2017-01-04 11:45:20",
"created_at" : "2017-01-04 11:45:20"
}
第2集: 任务
{
"_id" : ObjectId("586ccbcf1a72cb07a6815b04"),
"task_name" : "New Task",
"task_description" : "test description",
"task_status" : 1,
"task_created_at" : "2017-04-01 02:17:00",
"task_updated_at" : "2017-04-01 02:17:00"
},
{
"_id" : ObjectId("586cd3261a72cb07a6815c69"),
"task_name" : "2nd Task",
"task_description" : "test description",
"task_status" : 1,
"task_created_at" : "2017-04-01 02:17:00",
"task_updated_at" : "2017-04-01 02:17:00"
}
我在它们之间创建了枢轴table
Employee_Task
{
"_id" : ObjectId("586cd0cb1a72cb07a6815bf3"),
"employee_task_employee_id" : "586ca8c71a72cb07a681566d",
"employee_task_task_id" : "586ccbcf1a72cb07a6815b04",
"status" : 1
},
{
"_id" : ObjectId("586cd7851a72cb07a6815d7d"),
"employee_task_employee_id" : "586ca8c71a72cb07a681566d",
"employee_task_task_id" : "586cd3261a72cb07a6815c69",
"status" : 1
}
Laravel:
型号:
员工:
<?php
namespace App\Models;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class Employee extends Eloquent {
protected $collection = 'employee';
protected $primaryKey = '_id';
public function tasks()
{
return $this->hasMany('App\Models\Task');
}
}
任务:
<?php
namespace App\Models;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class Task extends Eloquent {
protected $collection = 'task';
protected $primaryKey = '_id';
public function employees()
{
return $this->belongsToMany('App\Models\Employee');
}
}
控制器:
public function EmployeeData($data)
{
$employee= Employee::find('586ca8c71a72cb07a681566d')->tasks;
echo "<pre>";
print_r($employee);
}
当我想查看针对员工的任务时,它会显示以下输出:
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
)
)
我该如何解决这个问题?
在 Mongo Eloquent 中创建多对多关系时你不需要有一个枢轴 table,那是 SQL 心态,在 mongo- eloquent 外键存储在数组中的多对多关系。 所以模型应该是这样的:
<?php namespace App\Models;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class Employee extends Eloquent {
protected $collection = 'employee';
protected $primaryKey = '_id';
public function tasks()
{
return $this->belongsToMany('App\Models\Task');
}
}
<?php namespace App\Models;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class Task extends Eloquent {
protected $collection = 'task';
protected $primaryKey = '_id';
public function employees()
{
return $this->belongsToMany('App\Models\Employee');
}
}
此外,您应该在尝试检索关系之前先加载它们
$employee= Employee::with('tasks')->find('586ca8c71a72cb07a681566d')->tasks;
您可以像在 hasMany 关系中那样保存关系
$employee->tasks()->save(new Task());
是的,在这种情况下您需要使用 belongsToMany 而不是 hasMany...