从父模型laravel mongodb扩展模型

时间:2016-09-17 17:51:25

标签: php mongodb laravel laravel-5.2

我在mongodb数据库中收集的文档:

    {
    "_id": "56140137-eb4f-3e3d-accd-4fadc33c1f6f",
    "source": "322c7dff-3601-3ae1-aa6b-bca6de3aee5b",
    "destination": "da87db49-54be-3939-b92d-e04de1acfed1",
    "time": "9:00:00 PM",
    "flightnumber": 4075,
    "referenceMaster": [
        {
            "seatcount": "10",
            "date": "2016-09-17",
            "_id": "b2c4ce5e-26f4-3693-8794-ac354e85ae7f",
        },
        {
            "seatcount": "10",
            "time": "6:49:00 PM",
            "date": "2016-09-24",
            "_id": "bf95f85b-f751-32d4-9548-fb3f855ebf38",
        }]
    }

我创建了3个模型:

<?php
  class Flight extends Moloquent
   {
protected $connection = 'mongodb';
protected $fillable = [
'source',
'destination',
'time',
'flightnumber',
'reference_master',
];
public function referenceMaster()
{
return $this->embedsMany(ReferenceMaster::class);
}
}

class ReferenceMaster extends Flight
{
protected $fillable = [
'seatcount',
'time',
'date',
'referenceDetail'
];
public function referenceDetail()
{
return $this->embedsMany(ReferenceDetail::class);
}
}

class ReferenceDetail extends ReferenceMaster
{
protected $fillable = [
'price',
'seatcount'
];
 }

如何从referenceMaster获取数据:

  [
  {
  "source": "322c7dff-3601-3ae1-aa6b-bca6de3aee5b",
  "destination": "da87db49-54be-3939-b92d-e04de1acfed1",
  "time": "9:00:00 PM",
  "seatcount": "10",
  "date": "2016-09-17",
  },
  {
  "source": "322c7dff-3601-3ae1-aa6b-bca6de3aee5b",
  "destination": "da87db49-54be-3939-b92d-e04de1acfed1",
  "seatcount": "10",
  "time": "6:49:00 PM",
  "date": "2016-09-24",
  "_id": "bf95f85b-f751-32d4-9548-fb3f855ebf38",
 }
 ]

并将referenceDetail添加到referenceMaster

之一

通常我需要从其他对象(模型)扩展对象(模型),如果现有的新值需要覆盖,并且需要在mongodb中的另一个数组的对象数组中添加对象

0 个答案:

没有答案