/** * Removes documents from collection * * @param array $query * * @return Result */ public function remove($query) { $result = new Result(); $bulk = new BulkWrite(); $bulk->delete($query, ['limit' => 0]); $dbResult = $this->executeBulkWrite($bulk); if ($dbResult->getDeletedCount() === 0) { $result->setError(Result::ERROR_CANNOT_DELETE_RECORD, implode('. ', $dbResult->getWriteErrors())); } return $result; }
/** * Removes document. * * @return Result */ public function delete() { $result = new Result(); if ($this->existsInProductCharacteristics() === true) { return $result->setError(Result::ERROR_CANNOT_UPDATE_RECORD, 'Unit present in product specifications'); } if ($this->existsInPropertyCollection() === true) { return $result->setError(Result::ERROR_CANNOT_UPDATE_RECORD, 'Unit has related properties'); } return parent::delete(); }
/** * Validate Specification. * * @param Specification $specification * * @return Result */ public function validateSpecification(Specification $specification) { $result = new Result(); if (empty($specification->getAttribute('value'))) { return $result->setError(Result::ERROR_VALIDATION_FAILED, 'Specification value empty'); } if ($specification->getAttribute('unit_id') !== null && $specification->getUnit() === null) { return $result->setError(Result::ERROR_VALIDATION_FAILED, 'Specification unit invalid'); } return $result; }
/** * Saves document * * @param bool $validationNeeded * * @return Result */ public function save($validationNeeded = true) { $result = new Result(); /** @var AbstractCollection $collection */ $collection = $this->getCollection(); if ($validationNeeded) { $validatorResult = $collection->validate($this->toArray()); if ($validatorResult->isValid() === false) { return $result->setError(Result::ERROR_VALIDATION_FAILED, $validatorResult->getErrorMessage()); } } return $collection->update(['_id' => $this->getId()], ['$set' => $this->toArray()]); }
/** * Tries to unassign property from Category * * If property exists in Products ot this Category then fails * * @param ObjectID $propertyId * * @return Result */ public function removeProperty(ObjectID $propertyId) { $result = new Result(); $existsInProducts = (bool) $this->getClient()->getCollection('Product')->countByAttributes(['specifications' => ['$elemMatch' => ['property_id' => $propertyId]]]); return $existsInProducts ? $result->setError(Result::ERROR_CANNOT_UPDATE_RECORD, 'Property exists in specifications in products') : $this->getCollection()->update(['_id' => $this->getId()], ['$pull' => ['properties' => $propertyId]]); }
/** * Adds Spec to product. * * @param Specification $specification * * @return Result */ public function addSpecification(Specification $specification) { $result = new Result(); $validatorResult = $specification->validate(); if ($validatorResult->isOk() == false) { return $result->setError(Result::ERROR_VALIDATION_FAILED, 'Specification invalid'); } $this->getCollection()->update(['_id' => $this->getId()], ['$pull' => ['specifications' => ['property_id' => $specification->getPropertyId()]]]); // TODO: Perform next action only in previous succeeded return $this->getCollection()->update(['_id' => $this->getId()], ['$push' => ['specifications' => $specification->toArray() + ['_id' => $specification->getId()]]]); }
/** * Binds DefaultValue to property. * * @param DefaultValue $defaultValue * * @return Result */ public function addDefaultValue(DefaultValue $defaultValue) { $result = new Result(); $validatorResult = $defaultValue->validate(); if ($validatorResult->isValid() == false) { return $result->setError(Result::ERROR_VALIDATION_FAILED, 'Default value invalid'); } return $this->getCollection()->update(['_id' => $this->getId()], ['$push' => ['default_values' => $defaultValue->toArray() + ['_id' => $defaultValue->getId()]]]); }
/** * @return Result */ public function validate() { $result = new Result(); $property = $this->getProperty(); return $property ? $property->getTypeHandler()->validateSpecification($this) : $result->setError(Result::ERROR_VALIDATION_FAILED, 'Property not found'); }