简体   繁体   中英

Sonata Admin Bundle - Custom field with URL

Is there way to show url with filter to another entity list instead of showing all related entities?

My entity has OneToMany reference to it's events:

/**
 *
 * @ORM\OneToMany(targetEntity="Event", mappedBy="organizer", cascade={"ALL"})
 */
private $events;

$formMapper->add('events') shows me select2 input with all events.

I just want to show a link to events list with filter to current owner.

I'm using Symfony 2.5.

Yes, this is possible.

You have to get the current owner and create a custom query builder to get only the events related to the owner identifier.

protected function configureFormFields(FormMapper $formMapper)
{
    // get current owner
    $ownerId = $this->subject->getId();
    // using query_builder to create a custom query based on current owner
    $formMapper->add('events', null, array(
        'query_builder' => function(EntityRepository $er) use ($ownerId) {
            $events = $er->createQueryBuilder('e');
            if ($ownerId != null) {
                $events = $er->where('e.owner = :ownerId')
                             ->setParameter('ownerId', $ownerId);
            }
            return $events;
        }
    ));
}

Also don't forget to add the use for EntityRepository :

use Doctrine\\ORM\\EntityRepository;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM