Sonata 2 display 2 fields in the same column

谁说我不能喝 提交于 2019-12-11 05:38:52

问题


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:

  1. Add a non mapped field, call it fullName.
  2. Add a PRE_SUBMIT form event listener which will populate fullName with firstName + LastName
  3. Add a POST_SUBMIT form event listener which will then parse fullName back into lastName and firstName
  4. Persist entity.


来源:https://stackoverflow.com/questions/27220524/sonata-2-display-2-fields-in-the-same-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!