0

I have a User Entity class that contains one-to-many property "transactions" that holds the transactions of that particular user.

use Sonata\UserBundle\Entity\BaseUser as BaseUser;
class User extends BaseUser
{
     //...//

     /*
      * @ORM\OneToMany(targetEntity="Transaction", mappedBy="user")
      *
      */
      protected $transactions;

     //...//
}

In the backend i have the userAdmin class that extends the sonata admin class

/**
 * @param \Sonata\AdminBundle\Datagrid\ListMapper $listMapper
 * @return void
 */
protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->add('id')
        ->addIdentifier('username')
        ->add('transactions')
    ;
}

If i do this i get the following error:

An exception has been thrown during the rendering of a template ("You must define an associated_tostring option or create a Project\MyBundle\Entity\Transaction::__toString method to the field option transactions from service gd_admin.customer_details is ") in SonataDoctrineORMAdminBundle:CRUD:list_orm_one_to_many.html.twig at line 17.

Not understanding how do i provide a link to the user transactions in the User Listing page. Any help would be really greatful.

Thanks.

VishwaKumar
  • 3,433
  • 8
  • 44
  • 72

2 Answers2

3

You need to implement __toString() method in Transaction class. It should return a string which meaningfully describes a transaction. Symfony will automatically call this method to get a description of the transaction to be displayed against the user. Following is an example implementation I used for a User class in one of my projects:

public function __toString()
{
    return ($this->firstName . ' ' . $this->lastName);
} 

Best Regards, Champika

0

All you have to do is either specify function __toString() on your entity or you have another option in the field specification you can say what property should be used as title for the entity, like so:

$listMapper
    ->add('id')
    ->addIdentifier('username')
    ->add('transactions', null, array('associated_property' => 'transaction_name'))
;

This is all assuming you have filed called transaction_name in Transaction entity or you can just use the ID.

Alex Rashkov
  • 9,833
  • 3
  • 32
  • 58