/** * Validates and saves the new or updated entity and returns the appropriate HTTP * response. * * @param Application $app * the current application * @param CRUDData $crudData * the data instance of the entity * @param CRUDEntity $instance * the entity * @param string $entity * the name of the entity * @param boolean $edit * whether to edit (true) or to create (false) the entity * * @return Response * the HTTP response of this modification */ protected function modifyEntity(Application $app, CRUDData $crudData, CRUDEntity $instance, $entity, $edit) { $fieldErrors = array(); $mode = $edit ? 'edit' : 'create'; if ($app['request']->getMethod() == 'POST') { $instance->populateViaRequest($app['request']); $validation = $instance->validate($crudData); if ($mode === 'edit' && intval($app['request']->get('version')) !== $instance->get('version')) { $validation['valid'] = false; $app['session']->getFlashBag()->add('danger', $app['translator']->trans('crudlex.edit.locked')); } $fieldErrors = $validation['fields']; if (!$validation['valid']) { $app['session']->getFlashBag()->add('danger', $app['translator']->trans('crudlex.' . $mode . '.error')); } else { $modified = $edit ? $crudData->update($instance) : $crudData->create($instance); if ($modified) { $id = $instance->get('id'); if ($edit) { $crudData->updateFiles($app['request'], $instance, $entity); } else { $crudData->createFiles($app['request'], $instance, $entity); } $app['session']->getFlashBag()->add('success', $app['translator']->trans('crudlex.' . $mode . '.success', array('%label%' => $crudData->getDefinition()->getLabel(), '%id%' => $id))); return $app->redirect($app['url_generator']->generate('crudShow', array('entity' => $entity, 'id' => $id))); } $app['session']->getFlashBag()->add('danger', $app['translator']->trans('crudlex.' . $mode . '.failed')); } } return $app['twig']->render($app['crud']->getTemplate($app, 'template', 'form', $entity), array('crudEntity' => $entity, 'crudData' => $crudData, 'entity' => $instance, 'mode' => $mode, 'fieldErrors' => $fieldErrors, 'layout' => $app['crud']->getTemplate($app, 'layout', $mode, $entity))); }
public function testGetSet() { $definition = $this->crudServiceProvider->getData('book')->getDefinition(); $entity = new CRUDEntity($definition); $entity->set('test', 'testdata'); $read = $entity->get('test'); $expected = 'testdata'; $this->assertSame($read, $expected); $entity->set('test', 'testdata2'); $read = $entity->get('test'); $expected = 'testdata2'; $this->assertSame($read, $expected); $read = $entity->get('testNull'); $this->assertNull($read); $entity->set('price', 3.99); $read = $entity->get('price'); $expected = 3.99; $this->assertSame($read, $expected); $entity->set('pages', 111); $read = $entity->get('pages'); $expected = 111; $this->assertSame($read, $expected); // Fixed values override $definition->setFixedValue('pages', 666); $entity->set('pages', 111); $read = $entity->get('pages'); $expected = 666; $this->assertSame($read, $expected); $definition = $this->crudServiceProvider->getData('book')->getDefinition(); $entity = new CRUDEntity($definition); $entity->set('isOpenOnSundays', true); $read = $entity->get('isOpenOnSundays'); $expected = true; $this->assertSame($read, $expected); }
public function testList() { $entity = $this->dataLibrary->createEmpty(); $entity->set('name', 'nameA'); $this->dataLibrary->create($entity); $entity = new CRUDEntity($this->dataBook->getDefinition()); $entity->set('name', 'nameB'); $this->dataLibrary->create($entity); $list = $this->dataLibrary->listEntries(); $read = count($list); $expected = 2; $this->assertSame($read, $expected); $list = $this->dataLibrary->listEntries(array('name' => 'nameB'), array('name' => '=')); $read = count($list); $expected = 1; $this->assertSame($read, $expected); $list = $this->dataLibrary->listEntries(array('name' => 'nameB', 'id' => 2), array('name' => '=', 'id' => '=')); $read = count($list); $expected = 1; $this->assertSame($read, $expected); $list = $this->dataLibrary->listEntries(array('type' => null), array('type' => '=')); $read = count($list); $expected = 2; $this->assertSame($read, $expected); $list = $this->dataLibrary->listEntries(array('name' => '%eB%'), array('name' => 'LIKE')); $read = count($list); $expected = 1; $this->assertSame($read, $expected); $list = $this->dataLibrary->listEntries(array(), array(), null, null, 'name'); $expected = 'nameB'; $this->assertSame($list[0]->get('name'), $expected); $expected = 'nameA'; $this->assertSame($list[1]->get('name'), $expected); for ($i = 0; $i < 15; ++$i) { $entity->set('name', 'name' . $i); $this->dataLibrary->create($entity); } $list = $this->dataLibrary->listEntries(array(), array(), null, null); $read = count($list); $expected = 17; $this->assertSame($read, $expected); $list = $this->dataLibrary->listEntries(array(), array(), null, 5); $read = count($list); $expected = 5; $this->assertSame($read, $expected); $list = $this->dataLibrary->listEntries(array(), array(), 0, 5); $read = count($list); $expected = 5; $this->assertSame($read, $expected); $list = $this->dataLibrary->listEntries(array(), array(), 15, 5); $read = count($list); $expected = 2; $this->assertSame($read, $expected); $list = $this->dataLibrary->listEntries(array(), array(), 5, null); $read = count($list); $expected = 12; $this->assertSame($read, $expected); }
/** * Sets the values and parameters of the upcoming given query according * to the entity. * * @param CRUDEntity $entity * the entity with its fields and values * @param QueryBuilder $queryBuilder * the upcoming query * @param boolean $setValue * whether to use QueryBuilder::setValue (true) or QueryBuilder::set (false) */ protected function setValuesAndParameters(CRUDEntity $entity, QueryBuilder $queryBuilder, $setValue) { $formFields = $this->definition->getEditableFieldNames(); $count = count($formFields); for ($i = 0; $i < $count; ++$i) { $value = $entity->get($formFields[$i]); $type = $this->definition->getType($formFields[$i]); if ($type == 'bool') { $value = $value ? 1 : 0; } if ($type == 'date' || $type == 'datetime' || $type == 'reference') { $value = $value == '' ? null : $value; } if ($setValue) { $queryBuilder->setValue('`' . $formFields[$i] . '`', '?'); } else { $queryBuilder->set('`' . $formFields[$i] . '`', '?'); } $queryBuilder->setParameter($i, $value); } }
/** * Creates a new, empty entity instance having all fields prefilled with * null or the defined value in case of fixed fields. * * @return CRUDEntity * the newly created entity */ public function createEmpty() { $entity = new CRUDEntity($this->definition); $fields = $this->definition->getEditableFieldNames(); foreach ($fields as $field) { $value = null; if ($this->definition->getType($field) == 'fixed') { $value = $this->definition->getFixedValue($field); } $entity->set($field, $value); } $entity->set('id', null); return $entity; }
/** * Constructs a file system path for the given parameters for storing the * file of the file field. * * @param string $entityName * the entity name * @param CRUDEntity $entity * the entity * @param string $field * the file field in the entity * * @return string * the constructed path for storing the file of the file field */ protected function getPath($entityName, CRUDEntity $entity, $field) { return $entity->getDefinition()->getFilePath($field) . '/' . $entityName . '/' . $entity->get('id') . '/' . $field; }