2016-02-05 34 views
0

首先,我是laravel 5.0中使用jenssegers的laravel mongodb集合的新手。我想,以形成子集裏收集像下面如何使用Laravel 5和mongodb創建子集合

{ 
    "Uid": 1112, 
    "AccountNo": 7620424, 
    "Party": { 
    "LanguageCode": "en", 
    "Type": "individual", 
    "Contact": [ 
     { 
     "id" : It will be mongo object id How to create? 
     "Type": "default", 
     "Person": { 
      "FullName": "Test Test" 
     }, 
     { 
     "id" : It will be mongo object id How to create? 
     "Type": "default", 
     "Person": { 
      "FullName": "Test Test" 
     }, 
     "MessagingMedium": [ 
      { 
      "Id": It will be mongo object id How to create? 
      "Scheme": "mailto", 
      "URI": "[email protected]", 
      "Status": "awaiting.registration" 
      }, 
      { 
      "Id": It will be mongo object id How to create? 
      "Scheme": "mailto", 
      "URI": "[email protected]", 
      "Status": "awaiting.registration" 
      } 
     ] 
     } 
    ] 
    }, 
} 

我USERPROFILE模型

use Jenssegers\Mongodb\Model as Eloquent; 

class UserProfile extends Eloquent { 

    protected $table = 'user_profile'; 
} 

use Jenssegers\Mongodb\Model as Eloquent; 

class Contact extends Eloquent { 

    protected $table = 'contact'; 
} 

我一直試圖嘗試接觸子集合添加到USERPROFILE集合創建像下面後創建的用戶配置文件

$user = UserProfile::create(array('Uid' => $uId, 'AccountNo' => $accNo)); 
$card = Contact::create(['id' => 123]); //contact mongodb Eloquent 
$card->userprofile()->save($card); 

但其沒有工作和收集也沒有創造

那是正確的,我不知道abount的hasMany,embedsmany colleciton

任何人幫我出

回答

0

您必須使用embedsMany relation。這種關係允許您將對象數組(使用自己的ID的Eloquent對象)保存到現有文檔中。該關係將完全由ORM管理。

你可以這樣聲明它:

class UserProfile extends Eloquent 
{ 
    // No table declaration because this model is only saved in the contact collection 
} 

class Contact extends Eloquent 
{ 
    protected $table = 'contact'; 

    public function userProfiles() 
    { 
     return $this->embedsMany('UserProfile'); 
    } 
} 

如果你想嵌入只有一個對象爲其他,則可以使用embedsOne關係。

而在此之前,您應該認真看待軟件包文檔;)

相關問題