2016-02-05 88 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关系。

而在此之前,您应该认真看待软件包文档;)