Michal Cygankiewicz
2017-07-18 09:22:09 UTC
I want to create viewHelper that will pass the whole domain object into the form (in other words generate hidden fields for the whole object). Example code like below.
The problem which I am facing now that it will render all the properties which may lead to errors like "#1297759968: Exception while property mapping at property path "": It is not allowed to map property "sorting". You need to use $propertyMappingConfiguration->allowProperties('sorting') to enable mapping of this property." on the target action.
My question is - how can I get only 'mappable' properties for given object in viewhelper context? I know it is done by PropertyMappingConfiguration in Controller context (shouldMap function). But is it possible to get same info also in viewhelper context?
/**
* @param AbstractEntity $object
* @param string $objectName
* @return string
*/
public function render(AbstractEntity $object, $objectName)
{
$tags = '';
foreach ($object->_getProperties() as $propertyKey => $propertyValue) {
$this->arguments['name'] = $objectName . '[' . $propertyKey . ']';
$this->tag->addAttribute('type', 'hidden');
$fieldName = $this->getName();
if ($propertyValue instanceof ObjectStorage) {
foreach ($propertyValue->toArray() as $key => $value) {
$this->tag->addAttribute('name', $fieldName . '[]');
$this->tag->addAttribute('value', $value->getUid());
$tags .= $this->tag->render();
}
} else if ($propertyValue) {
$this->tag->addAttribute('name', $fieldName);
$this->tag->addAttribute('value', $propertyValue);
$tags .= $this->tag->render();
}
}
return $tags;
}
The problem which I am facing now that it will render all the properties which may lead to errors like "#1297759968: Exception while property mapping at property path "": It is not allowed to map property "sorting". You need to use $propertyMappingConfiguration->allowProperties('sorting') to enable mapping of this property." on the target action.
My question is - how can I get only 'mappable' properties for given object in viewhelper context? I know it is done by PropertyMappingConfiguration in Controller context (shouldMap function). But is it possible to get same info also in viewhelper context?
/**
* @param AbstractEntity $object
* @param string $objectName
* @return string
*/
public function render(AbstractEntity $object, $objectName)
{
$tags = '';
foreach ($object->_getProperties() as $propertyKey => $propertyValue) {
$this->arguments['name'] = $objectName . '[' . $propertyKey . ']';
$this->tag->addAttribute('type', 'hidden');
$fieldName = $this->getName();
if ($propertyValue instanceof ObjectStorage) {
foreach ($propertyValue->toArray() as $key => $value) {
$this->tag->addAttribute('name', $fieldName . '[]');
$this->tag->addAttribute('value', $value->getUid());
$tags .= $this->tag->render();
}
} else if ($propertyValue) {
$this->tag->addAttribute('name', $fieldName);
$this->tag->addAttribute('value', $propertyValue);
$tags .= $this->tag->render();
}
}
return $tags;
}