I did not work with SonataAdminBundle forms, but I think it works absolutely like symfony forms. All you need to do is add the values โโof 'class' and 'property' to the parameter list:
$form->with('Item')->add('parent', null, array( 'class' => 'Acme\DemoBundle\Entity\Category', 'property' => 'url', 'label' => 'Category', 'required' => true, 'query_builder' => function($er) use ($id) { $qb = $er->createQueryBuilder('p'); if ($id){ $qb->where('p.id <> :id') ->setParameter('id', $id); } $qb->orderBy('p.root, p.lft', 'ASC'); return $qb; }
property is the name of the field in your entity that will represent your Entity value instead of calling __toString() . But also ... If you always need to present your Entity as a URL, you can simply redefine the __toString() method in the Entity class to something like this:
public function __toString() { return $this->url; }
source share