/** * Insert this model into the repository. * * @throws ModelValidationException * @return bool */ protected function insert() { $validated = $this->validate([], MongaCollection::getMask($this, [], [$this->getKey()])); if (true !== $validated) { throw new ModelValidationException('model validation failed for new item in collection ' . static::$collectionName, $validated); } $saved = static::collection($this->getRepository())->insert($this->toArray(MongaCollection::getMask($this, [], [$this->getKeyField()]), false), ['w' => 1]); if ($saved instanceof \MongoId) { $this->{$this->getKeyField()} = $saved; return true; } return false; }
/** * Test a GET request for a RestfulItem with fields specified to return. */ public function testGetWithFields() { /** @var MongaPersistentObject $itemObj */ $itemObj = MongaPersistentObject::findOne(['name' => 'MockPersistent #1'], [], $this->fixture); $this->mockEnvironment(['PATH_INFO' => '/monga/collection/' . $itemObj->_id, 'QUERY_STRING' => 'fields=name,text', 'REQUEST_METHOD' => 'GET']); try { $response = $this->tacit->invoke(); $item = json_decode($response->getBody(), true); $data = $itemObj->toArray(MongaCollection::getMask($itemObj, ['integer', 'float', 'date', 'boolean', 'arrayOfStrings'])); $matches = array_uintersect_assoc($data, $item, array($this, 'compareMultidimensionalArray')); $this->assertEquals($data, $matches); } catch (RestfulException $e) { $this->fail($e->getMessage()); } }
public function transform(MongaPersistent $resource) { return array_merge(['id' => (string) $resource->_id], $resource->toArray(MongaCollection::getMask($resource))); }