/** * @depends testMockRequest * @param $request */ public function testGetRelation($request) { $relation = Params::getRelation($request, 'R1'); $this->assertEquals($relation, $this->post['relations']['R1'], 'relation Ri expected value'); $request = $this->getMockBuilder(Request::class)->getMock(); $request->method('getPost')->willReturn(array()); $relation = Params::getRelation($request, 'R1'); $this->assertEquals($relation, null, 'relation is null'); $request = $this->getMockBuilder(Request::class)->getMock(); $request->method('getPost')->willReturn(array('relations' => array('R1' => array()))); $relation = Params::getRelation($request, 'R1'); $this->assertEquals($relation, null, 'relation is null'); }
/** * @param \Phalcon\Mvc\Model\Query\Builder $builder * @param $params */ private static function setColumns($builder, $params) { $columns = \PPA\Rest\Utils\Params::getColumns($params); $excludeColumns = \PPA\Rest\Utils\Params::getExcludeColumns($params); if ($columns) { $builder->columns($columns); } if ($excludeColumns) { self::setExcludeColumns($builder, $excludeColumns); } }
/** * @param array $relationsArray * @param \Phalcon\Mvc\Model $model * @param \Phalcon\Mvc\Model\Relation $relation */ public function save(array $relationsArray, $model, $relation) { $relationAlias = $relation->getOption('alias'); $needDeleteRelations = $this->getNeedDelete($relationsArray, $model, $relation); $this->delete($needDeleteRelations); foreach ($relationsArray as $relationData) { $relationData = Params::convertDate($relationData, $this->getDI()); if (empty($relationData['id'])) { $this->create($relationData, $model, $relationAlias); } else { $this->update($relationData, $model, $relationAlias); } } }
/** * Delete record from DB. * @return array */ private function delete() { $params = Params::getMergeParams($this->request); if (empty($params['id'])) { return array('success' => false, 'msg' => 'param id is require'); } $id = intval($params['id']); $modelName = Analyzer::getModelName($this->request->get('_url')); /** * @var \Phalcon\Mvc\Model $model */ $model = $modelName::findFirst($id); if (!$model) { return array('success' => false, 'msg' => 'Record with id ' . $id . ' not found'); } $this->securityManager->check(array('model' => $model, 'action' => CrudOperations::DELETE, 'modelName' => $modelName, 'params' => $params)); $this->logManager->deleteModel($model); if ($model->delete()) { $this->logManager->deleteModel($model); return array('success' => true, 'msg' => 'Record with id ' . $id . ' has removed!'); } return array('success' => false, 'msg' => $this->jsonRecursiveGetMsg($model->getMessages())); }