2017-01-07 108 views
0

我正在使用MongoDB庫012​​version 3.1.0-alpha in Laravel 5.3.28我在MongoDB中有兩個集合,我想使它們具有許多關係。意味着每個員工執行許多任務。我已經使用了引用並在任務集合中添加了employee_ids。Laravel MongoDB庫'jenssegers/laravel-mongodb'hasMany relationship is not working

下面是我的代碼:

的MongoDB:

1集:員工

{ 
    "_id" : ObjectId("586ca8c71a72cb07a681566d"), 
    "employee_name" : "John", 
    "employee_description" : "test description", 
    "employee_email" : "[email protected]", 
    "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" : "[email protected]", 
    "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_start" : "2017-04-01 12:00:00", 
    "task_end" : "2017-04-01 02:00:00", 
    "task_created_at" : "2017-04-01 02:17:00", 
    "task_updated_at" : "2017-04-01 02:17:00", 
    "employee_id" : [ 
     ObjectId("586ca8c71a72cb07a681566d"), 
     ObjectId("586ca8d31a72cb07a6815671") 
    ] 
}, 
{ 
    "_id" : ObjectId("586cd3261a72cb07a6815c69"), 
    "task_name" : "2nd Task", 
    "task_description" : "test description", 
    "task_status" : 1, 
    "task_start" : "2017-04-01 12:00:00", 
    "task_end" : "2017-04-01 02:00:00", 
    "task_created_at" : "2017-04-01 02:17:00", 
    "task_updated_at" : "2017-04-01 02:17:00", 
    "employee_id" : ObjectId("586ca8c71a72cb07a681566d") 
} 

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'); 
    } 
} 

Laravel: 型號: 任務:

<?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->belongsTo('App\Models\Employee'); 
    } 
} 

我想分配給特定員工的任務。

控制器:

public function EmployeeData($data) 
{ 
    $employees = Employee::with('tasks')->where('_id', new \MongoDB\BSON\ObjectID('586ca8d31a72cb07a6815671'))->get(); 
     echo "<pre>"; 
     print_r($employees);exit; 
} 

輸出:

Illuminate\Database\Eloquent\Collection Object 
(
    [items:protected] => Array 
     (
      [0] => App\Models\Employee Object 
       (
        [connection:protected] => mongodb 
        [collection:protected] => lt_employees 
        [primaryKey:protected] => _id 
        [employee_id:App\Models\Employee:private] => 
        [employee_name:App\Models\Employee:private] => 
        [employee_description:App\Models\Employee:private] => 
        [employee_email:App\Models\Employee:private] => 
        [employee_created_at:App\Models\Employee:private] => 
        [employee_updated_at:App\Models\Employee:private] => 
        [parentRelation:protected] => 
        [table:protected] => 
        [keyType:protected] => int 
        [perPage:protected] => 15 
        [incrementing] => 1 
        [timestamps] => 1 
        [attributes:protected] => Array 
         (
          [_id] => MongoDB\BSON\ObjectID Object 
           (
            [oid] => 586ca8d31a72cb07a6815671 
           ) 

          [employee_name] => Carlos 
          [employee_description] => test description 
          [employee_email] => [email protected] 
          [updated_at] => 2017-01-04 11:45:20 
          [created_at] => 2017-01-04 11:45:20 
         ) 

        [original:protected] => Array 
         (
          [_id] => MongoDB\BSON\ObjectID Object 
           (
            [oid] => 586ca8d31a72cb07a6815671 
           ) 

          [employee_name] => Carlos 
          [employee_description] => test description 
          [employee_email] => [email protected] 
          [updated_at] => 2017-01-04 11:45:20 
          [created_at] => 2017-01-04 11:45:20 
         ) 

        [relations:protected] => Array 
         (
          [tasks] => Illuminate\Database\Eloquent\Collection Object 
           (
            [items:protected] => Array 
             (
             ) 

           ) 

         ) 

        [hidden:protected] => Array 
         (
         ) 

        [visible:protected] => Array 
         (
         ) 

        [appends:protected] => Array 
         (
         ) 

        [fillable:protected] => Array 
         (
         ) 

        [guarded:protected] => Array 
         (
          [0] => * 
         ) 

        [dates:protected] => Array 
         (
         ) 

        [dateFormat:protected] => 
        [casts:protected] => Array 
         (
         ) 

        [touches:protected] => Array 
         (
         ) 

        [observables:protected] => Array 
         (
         ) 

        [with:protected] => Array 
         (
         ) 

        [exists] => 1 
        [wasRecentlyCreated] => 
       ) 

     ) 

) 

在輸出中,關係任務項是空的。

任何人都可以告訴我,關係黑白集合是正確的嗎?

更新

我已經使用belongsToManyin的關係。現在我的型號有:

在員工型號:

public function tasks() 
    { 
     return $this->belongsToMany('App\Models\Task'); 
    } 

在任務模式:

public function employees() 
    { 
     return $this->belongsToMany('App\Models\Employee'); 
    } 

這些文件:

員工收集

{ 
    "_id" : ObjectId("586ca8c71a72cb07a681566d"), 
    "employee_name" : "Carlos", 
    "employee_description" : "test description", 
    "employee_email" : "[email protected]", 
    "updated_at" : "2017-01-04 11:45:20", 
    "created_at" : "2017-01-04 11:45:20", 
    "task_ids" : [ 
     ObjectId("586ccbcf1a72cb07a6815b04"), 
     ObjectId("586cd3261a72cb07a6815c69") 
    ] 
}, 
{ 
    "_id" : ObjectId("586ca8d31a72cb07a6815671"), 
    "employee_name" : "John", 
    "employee_description" : "test description", 
    "employee_email" : "[email protected]", 
    "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_start" : "2017-04-01 12:00:00", 
    "task_end" : "2017-04-01 02:00:00", 
    "task_created_at" : "2017-04-01 02:17:00", 
    "task_updated_at" : "2017-04-01 02:17:00", 
    "employee_ids" : [ 
     ObjectId("586ca8c71a72cb07a681566d"), 
     ObjectId("586ca8d31a72cb07a6815671") 
    ] 
}, 
{ 
    "_id" : ObjectId("586cd3261a72cb07a6815c69"), 
    "task_name" : "2nd Task", 
    "task_description" : "test description", 
    "task_status" : 1, 
    "task_start" : "2017-04-01 12:00:00", 
    "task_end" : "2017-04-01 02:00:00", 
    "task_created_at" : "2017-04-01 02:17:00", 
    "task_updated_at" : "2017-04-01 02:17:00", 
    "employee_ids" : ObjectId("586ca8c71a72cb07a681566d") 
} 

我與這些文件的第一個員工:

$employee = Employee::with('tasks')->first(); 
dd($employee); 

我gotthe空關係輸出:

Employee {#176 
    #connection: "mongodb" 
    #collection: "employee" 
    #primaryKey: "_id" 
    -employee_id: null 
    -employee_name: null 
    -employee_description: null 
    -employee_email: null 
    -employee_created_at: null 
    -employee_updated_at: null 
    #parentRelation: null 
    #table: null 
    #keyType: "int" 
    #perPage: 15 
    +incrementing: true 
    +timestamps: true 
    #attributes: array:10 [ 
    "_id" => ObjectID {#170} 
    "employee_name" => "Carlos" 
    "employee_description" => "test description" 
    "employee_email" => "[email protected]" 
    "updated_at" => "2017-01-04 11:45:20" 
    "created_at" => "2017-01-04 11:45:20" 
    "task_ids" => array:2 [ 
     0 => ObjectID {#174} 
     1 => ObjectID {#175} 
    ] 
    ] 
    #original: array:10 [ 
    "_id" => ObjectID {#170} 
    "employee_name" => "Carlos" 
    "employee_description" => "test description" 
    "employee_email" => "[email protected]" 
    "updated_at" => "2017-01-04 11:45:20" 
    "created_at" => "2017-01-04 11:45:20" 
    "task_ids" => array:2 [ 
     0 => ObjectID {#174} 
     1 => ObjectID {#175} 
    ] 
    ] 
    #relations: array:1 [ 
    "tasks" => Collection {#173 
     #items: [] 
    } 
    ] 
    #hidden: [] 
    #visible: [] 
    #appends: [] 
    #fillable: [] 
    #guarded: array:1 [ 
    0 => "*" 
    ] 
    #dates: [] 
    #dateFormat: null 
    #casts: [] 
    #touches: [] 
    #observables: [] 
    #with: [] 
    +exists: true 
    +wasRecentlyCreated: false 
} 

回答

2

我明白由你的其他人問題是,一項任務可以屬於很多員工,對嗎?所以你應該在你的Task模型中使用belongsToMany關係。此外,您的示例「任務」集合顯示在一個文檔中,employee_id是一個數組,而在另一個文檔中,它是一個ObjectId,兩者都應該是數組。

無論如何,我有一個困難時期試圖弄清楚這一點,但我看到的是你不能使用hasManybelongsToMany逆,因爲belongsToMany創建一組ID,並hasMany不與陣列配合良好。我會說我們需要像hasManyInArray這樣的東西,但是當我關聯一個belongsToMany關係時,「父」文檔會創建一個id數組,這使我認爲父應該也使用belongsToMany,即使它不「屬於「但實際上」有「。所以,當你的員工像這樣的任務關聯起來:

$task->employees()->save($employee); 

「員工」的文件最終將不得不與唯一的任務ID應該有一個「task_ids」屬性。所以,這似乎是一起去Jenssegers方式:

Laravel:型號:員工:

<?php 
namespace App\Models; 

use Jenssegers\Mongodb\Eloquent\Model as Eloquent; 

class Employee extends Eloquent 
{ 
    protected $collection = 'employee'; 

    public function tasks() 
    { 
     return $this->belongsToMany(Task::class); 
    } 
} 

Laravel:型號:任務:

<?php 
namespace App\Models; 

use Jenssegers\Mongodb\Eloquent\Model as Eloquent; 

class Task extends Eloquent 
{ 
    protected $collection = 'task'; 

    public function employees() 
    { 
     return $this->belongsToMany(Employee::class); 
    } 
} 
在兩個模型使用 belongsToMany

你可以用這個:

// Give a task a new employee 
$task->employees()->save($employee); 

// Or give an employee a new task 
$employee->tasks()->save($task); 

唯一的一點是,當您查看數據庫時,您會看到您的員工文檔有一個名爲「task_ids」的數組,並且裏面有每個員工唯一任務的ID。我希望這有助於。


只是一些旁註,你知道你不必在每個模型上定義主鍵的名字,對吧?你不需要這樣的:

protected $primaryKey = '_id'; 

你也不必定義集合的名稱(即protected $collection = 'employee';),除非你真的希望他們能在單數(默認情況下它們是複數)。


我在半夜爬起來(這是上午03時52分在這裏),並檢查了電腦上的東西,然後檢查SO的看到你的問題,我希望這一次我回答很快爲您,我們似乎在不同的時區。

更新

這些是我的測試中創建的文件:

員工收集

{ 
    "_id" : ObjectId("5870ba1973b55b03d913ba54"), 
    "name" : "Jon", 
    "updated_at" : ISODate("2017-01-07T09:51:21.316Z"), 
    "created_at" : ISODate("2017-01-07T09:51:21.316Z"), 
    "task_ids" : [ 
     "5870ba1973b55b03d913ba56" 
    ] 
}, 
{ 
    "_id" : ObjectId("5870ba1973b55b03d913ba55"), 
    "name" : "Doe", 
    "updated_at" : ISODate("2017-01-07T09:51:21.317Z"), 
    "created_at" : ISODate("2017-01-07T09:51:21.317Z"), 
    "task_ids" : [ 
     "5870ba1973b55b03d913ba56" 
    ] 
} 

任務收集

{ 
    "_id" : ObjectId("5870ba1973b55b03d913ba56"), 
    "name" : "New Task", 
    "updated_at" : ISODate("2017-01-07T09:51:21.317Z"), 
    "created_at" : ISODate("2017-01-07T09:51:21.317Z"), 
    "employee_ids" : [ 
     "5870ba1973b55b03d913ba54", 
     "5870ba1973b55b03d913ba55" 
    ] 
} 

這些文件我得到的第一個員工是這樣的:

$employee = Employee::with('tasks')->first(); 
dd($employee); 

,並在輸出我們可以看到關係屬性是一個數組:

Employee {#186 ▼ 
    #collection: "employee" 
    #primaryKey: "_id" 
    // Etc..... 
    #relations: array:1 [▼ 
    "tasks" => Collection {#199 ▼ 
     #items: array:1 [▼ 
     0 => Task {#198 ▼ 
      #collection: "task" 
      #primaryKey: "_id" 
      // Etc.... 
      #attributes: array:5 [▼ 
      "_id" => ObjectID {#193} 
      "name" => "New Task" 
      "updated_at" => UTCDateTime {#195} 
      "created_at" => UTCDateTime {#197} 
      "employee_ids" => array:2 [▶] 
      ] 
     } 
     ] 
    } 
    ] 
} 

更新2

belongsToMany方法不在您提到的文件中bec這種類(即Jenssegers\Mongodb\Eloquent\Model)擴展了Laravel的Eloquent Model類,這就是belongsToMany方法所在的地方。

好吧,這一定是爲什麼它不適合你,因爲數組必須是字符串而不是ObjectIds。爲什麼是這樣?因爲這就是Jenssegers圖書館的工作原理,所以它將Ids保存爲字符串。我也發現這種行爲很奇怪,但這就是它的工作原理。請記住,應該是以使用Jenssegers庫關聯對象,而不是通過在數據庫中手動創建數據。 你如何索引ID?只需在MongoDB中創建一個正常的索引,如tasks.createIndex({task_ids: 1})。以下是有關如何創建索引的文檔:https://docs.mongodb.com/manual/reference/method/db.collection.createIndex/。您也可以在遷移時創建索引,here are the docs on migrations,請務必閱讀Jenssegers notes on migrations

您可以像這樣訪問tasks的實現:$employee->tasks;。您可以通過獲取屬性與你宣佈你的關係的方法相同的名稱訪問的關係,所以如果你有:

class Post 
{ 
    public function owner() 
    { 
     return $this->belongsTo(User::class); 
    } 
} 

你得到的關係作爲$post->owner;。以下是有關關係的文件:https://laravel.com/docs/5.3/eloquent-relationships

+0

感謝您的回覆。現在,我已經在員工集合中的兩個模型和task_ids文檔中使用了'belongsToMany',就像您在上面的答案中所寫的一樣,但它不起作用。它給我發送相同的輸出 – okconfused

+0

你能否用你的新文件更新你的問題?我將用我創建的用於測試的文檔更新我的答案,這些文檔正在工作。 – Parziphal

+0

我已經更新了這個問題。通過使用'Model'中的文件'Jenssegers \ Mongodb \ Eloquent \ Model'告訴我有一件事我們正在使用'belongsToMany'關係,但在這個文件中'belongsToMany'關係不存在。 – okconfused