问题
I display the list form of Sonata. I have 2 fields : firstname, lastname. I want to display 2 fields in the same column.
Currently, I'm doing
$listMapper->add('firstname', 'text', array('label' => 'First Name'))
->add('lastname', 'text', array('label' => 'Last Name'));
How can I combine 2 fields in one without to change the Entity
回答1:
This is how i do it:
Say firstname
and lastname
are properties of User. In your entity class User, just add:
/**
* @return string
*/
public function getFullname()
{
return sprintf("%s %s", $this->getFirstname(), $this->getLastname());
}
Then in your admin class:
/**
* {@inheritdoc}
*/
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
...
->add('fullname', null, array('label' => 'Full Name'))
}
回答2:
You could possibly use a data transformer.
Or (and this doesn't seem like a good idea to me at all)you could do something like:
- Add a non mapped field, call it fullName.
- Add a PRE_SUBMIT form event listener which will populate fullName with firstName + LastName
- Add a POST_SUBMIT form event listener which will then parse fullName back into lastName and firstName
- Persist entity.
来源:https://stackoverflow.com/questions/27220524/sonata-2-display-2-fields-in-the-same-column