在Laravel 5.3 + MongoDB库'jenssegers/laravel-mongodb'中的hasMany关系问题 [英] hasMany relationship issue in Laravel 5.3 + MongoDB library 'jenssegers/laravel-mongodb'
问题描述
我正在使用Laravel 5.3
中的 MongoDB
库.我在MongoDB
中有两个集合,我想与它们建立hasMany
关系.
I am using MongoDB
library in Laravel 5.3
. I have two collections in MongoDB
and I want to make a hasMany
relation b/w them.
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"
}
第二个收藏集: 任务
{
"_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"
}
我已经在它们之间创建了数据透视表
I have created pivot table between them
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);
}
当我想查看针对员工的任务时,它会在输出下方显示我:
When I want to see task against employee it shows me below output:
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
)
)
我该如何解决?
推荐答案
在Mongo Eloquent中创建多对多关系时,您不需要拥有数据透视表,那就是SQL心态,而在Mongo雄辩式多对多关系中,外键存储在数组中. 因此模型应如下所示:
in Mongo Eloquent when creating Many to Many relationships you dont need to have a pivot table, thats SQL mindset, in mongo-eloquent many to many relations the foreign keys are stored in arrays. So the models should look like this:
<?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');
}
}
此外,您还应该在尝试检索关系之前先加载它们
Also you should load the relations before trying to retrieve them
$employee= Employee::with('tasks')->find('586ca8c71a72cb07a681566d')->tasks;
您可以像在hasMany关系中一样保存关系
You can save the relation the same way you do it in the hasMany relation
$employee->tasks()->save(new Task());
这篇关于在Laravel 5.3 + MongoDB库'jenssegers/laravel-mongodb'中的hasMany关系问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!