Example:
$path = new PropertyPath('child.name');
echo $path->setValue($object, 'Fabien');
equals echo $object->getChild()->setName('Fabien');
This method first tries to find a public setter for each property in the
path. The name of the setter must be the camel-cased property name
prefixed with "set".
If the setter does not exist, this method tries to find a public
property. The value of the property is then changed.
If neither is found, an exception is thrown. /** * Renders a pagerfanta. * * @param PagerfantaInterface $pagerfanta The pagerfanta. * @param string $viewName The view name. * @param array $options An array of options (optional). * * @return string The pagerfanta rendered. */ public function renderPagerfanta(PagerfantaInterface $pagerfanta, $viewName = 'default', array $options = array()) { $options = array_replace(array('routeName' => null, 'routeParams' => array(), 'pageParameter' => 'page'), $options); $router = $this->container->get('router'); $request = $this->container->get('request'); if (null === $options['routeName']) { $options['routeName'] = $request->attributes->get('_route'); $options['routeParams'] = $request->query->all(); if ($options['routeName'] === '_internal') { throw new \Exception('PagerfantaBundle can not guess the route when used in a subrequest'); } foreach ($router->getRouteCollection()->get($options['routeName'])->compile()->getVariables() as $variable) { $options['routeParams'][$variable] = $request->attributes->get($variable); } } $routeName = $options['routeName']; $routeParams = $options['routeParams']; $pagePropertyPath = new PropertyPath($options['pageParameter']); $routeGenerator = function ($page) use($router, $routeName, $routeParams, $pagePropertyPath) { $pagePropertyPath->setValue($routeParams, $page); return $router->generate($routeName, $routeParams); }; return $this->container->get('white_october_pagerfanta.view_factory')->get($viewName)->render($pagerfanta, $routeGenerator, $options); }
/** * @expectedException Symfony\Component\Form\Exception\InvalidPropertyException */ public function testMapFormToDataFailsIfOnlyRemoverFound() { $car = $this->getMock(__CLASS__ . '_CarOnlyRemover'); $axesBefore = $this->getCollection(array(1 => 'second', 3 => 'fourth')); $axesAfter = $this->getCollection(array(0 => 'first', 1 => 'second', 2 => 'third')); $path = new PropertyPath('axes'); $car->expects($this->any())->method('getAxes')->will($this->returnValue($axesBefore)); $path->setValue($car, $axesAfter); }
/** * @param \Symfony\Component\HttpFoundation\Request $request * @return \Symfony\Component\HttpFoundation\Response */ public function setObjectFieldValueAction(Request $request) { $field = $request->get('field'); $code = $request->get('code'); $objectId = $request->get('objectId'); $value = $request->get('value'); $context = $request->get('context'); $admin = $this->pool->getInstance($code); // alter should be done by using a post method if ($request->getMethod() != 'POST') { return new Response(json_encode(array('status' => 'KO', 'message' => 'Expected a POST Request')), 200, array('Content-Type' => 'application/json')); } $object = $admin->getObject($objectId); if (!$object) { return new Response(json_encode(array('status' => 'KO', 'message' => 'Object does not exist')), 200, array('Content-Type' => 'application/json')); } // check user permission if (false === $admin->isGranted('EDIT', $object)) { return new Response(json_encode(array('status' => 'KO', 'message' => 'Invalid permissions')), 200, array('Content-Type' => 'application/json')); } if ($context == 'list') { $fieldDescription = $admin->getListFieldDescription($field); } else { return new Response(json_encode(array('status' => 'KO', 'message' => 'Invalid context')), 200, array('Content-Type' => 'application/json')); } if (!$fieldDescription) { return new Response(json_encode(array('status' => 'KO', 'message' => 'The field does not exist')), 200, array('Content-Type' => 'application/json')); } if (!$fieldDescription->getOption('editable')) { return new Response(json_encode(array('status' => 'KO', 'message' => 'The field cannot be edit, editable option must be set to true')), 200, array('Content-Type' => 'application/json')); } // TODO : call the validator component ... $propertyPath = new PropertyPath($field); $propertyPath->setValue($object, $value); $admin->update($object); // render the widget // todo : fix this, the twig environment variable is not set inside the extension ... $extension = $this->twig->getExtension('sonata_admin'); $extension->initRuntime($this->twig); $content = $extension->renderListElement($object, $fieldDescription); return new Response(json_encode(array('status' => 'OK', 'content' => $content)), 200, array('Content-Type' => 'application/json')); }
public function testSetValueThrowsExceptionIfEmpty() { $path = new PropertyPath('foobar'); $value = ''; $this->setExpectedException('Symfony\\Component\\Form\\Exception\\UnexpectedTypeException'); $path->setValue($value, 'bam'); }
/** * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException */ public function testSetValueThrowsExceptionIfEmpty() { $path = new PropertyPath('foobar'); $value = ''; $path->setValue($value, 'bam'); }
/** * Build the form FieldDescription collection * * @return void */ protected function buildForm() { if ($this->form) { return; } // append parent object if any // todo : clean the way the Admin class can retrieve set the object if ($this->isChild() && $this->getParentAssociationMapping()) { $parent = $this->getParent()->getObject($this->request->get($this->getParent()->getIdParameter())); $propertyPath = new PropertyPath($this->getParentAssociationMapping()); $object = $this->getSubject(); $propertyPath->setValue($object, $parent); } $this->form = $this->getFormBuilder()->getForm(); }