Example:
$path = new PropertyPath('child.name');
echo $path->getValue($object);
equals echo $object->getChild()->getName();
This method first tries to find a public getter for each property in the
path. The name of the getter must be the camel-cased property name
prefixed with "get" or "is".
If the getter does not exist, this method tries to find a public
property. The value of the property is then returned.
If neither is found, an exception is thrown. public function testGetValueThrowsExceptionIfPropertyDoesNotExist() { $path = new PropertyPath('foobar'); $this->setExpectedException('Symfony\\Component\\Form\\Exception\\InvalidPropertyException'); $path->getValue(new Author()); }
/** * Initializes the choices and returns them * * The choices are generated from the entities. If the entities have a * composite identifier, the choices are indexed using ascending integers. * Otherwise the identifiers are used as indices. * * If the entities were passed in the "choices" option, this method * does not have any significant overhead. Otherwise, if a query builder * was passed in the "query_builder" option, this builder is now used * to construct a query which is executed. In the last case, all entities * for the underlying class are fetched from the repository. * * If the option "property" was passed, the property path in that option * is used as option values. Otherwise this method tries to convert * objects to strings using __toString(). * * @return array An array of choices */ protected function getInitializedChoices() { if ($this->getOption('choices')) { $entities = parent::getInitializedChoices(); } else { if ($qb = $this->getQueryBuilder()) { $entities = $qb->getQuery()->execute(); } else { $class = $this->getOption('class'); $em = $this->getOption('em'); $entities = $em->getRepository($class)->findAll(); } } $propertyPath = null; $choices = array(); $this->entities = array(); // The propery option defines, which property (path) is used for // displaying entities as strings if ($this->getOption('property')) { $propertyPath = new PropertyPath($this->getOption('property')); } foreach ($entities as $key => $entity) { if ($propertyPath) { // If the property option was given, use it $value = $propertyPath->getValue($entity); } else { // Otherwise expect a __toString() method in the entity $value = (string) $entity; } if (count($this->getIdentifierFields()) > 1) { // When the identifier consists of multiple field, use // naturally ordered keys to refer to the choices $choices[$key] = $value; $this->entities[$key] = $entity; } else { // When the identifier is a single field, index choices by // entity ID for performance reasons $id = current($this->getIdentifierValues($entity)); $choices[$id] = $value; $this->entities[$id] = $entity; } } return $choices; }