Sonata Admin-更改过滤器值(关系字段)

时间:2018-11-05 08:04:31

标签: php symfony autocomplete sonata-admin sonata

在我的后台(由 Sonata Admin 创建)中,我对注册进行了概述。

我的注册模型如下:

<?php

namespace MyProject\Domain\Model;

use MyProject\Domain\Model\Interfaces\HostInterface;
use MyProject\Domain\Model\Interfaces\VisitReasonInterface;
use MyProject\Domain\Model\Interfaces\CustomerInterface;
use MyProject\Domain\Model\Interfaces\EntryPointInterface;
use MyProject\Domain\Model\Interfaces\LanguageInterface;
use MyProject\Domain\Model\Interfaces\RegistrationInterface;
use MyProject\Domain\Model\Interfaces\SiteInterface;
use MyProject\Domain\Model\Traits\HasFileTrait;
use MyProject\Infrastructure\Model\GenderType;
use Knp\DoctrineBehaviors\Model\Timestampable\Timestampable;
use Symfony\Component\Intl\Intl;

/**
 * Class Registration.
 */
class Registration implements RegistrationInterface
{
    use HasFileTrait;
    use Timestampable;

    public const UPLOADS_FOLDER = 'uploads/registrations';

    /**
     * @var string|null
     */
    protected $companyName;

    /**
     * @var string|null
     */
    protected $firstname;

    /**
     * @var string|null
     */
    protected $lastname;

    /**
     * @var string|null
     */
    protected $email;

    /**
     * @var string|null
     */
    protected $addressFirstLine;

    /**
     * @var string|null
     */
    protected $addressSecondLine;

    /**
     * @var string|null
     */
    protected $zipCode;

    /**
     * @var string|null
     */
    protected $city;

    /**
     * @var string|null
     */
    protected $countryCode = 'BE';

    /**
     * @var string|null
     */
    protected $gender;

    /**
     * @var \DateTime|null
     */
    protected $birthDate;

    /**
     * @var string|null
     */
    protected $country = 'BE';

    /**
     * @var string|null
     */
    protected $website;

    /**
     * @var string|null
     */
    protected $workPermit;

    /**
     * @var string|null
     */
    protected $licensePlate;

    /**
     * @var bool
     */
    protected $incognito = false;

    /**
     * @var bool
     */
    protected $refused = false;

    /**
     * @var \DateTime
     */
    protected $eta;

    /**
     * @var \DateTime
     */
    protected $etd;

    /**
     * @var \DateTime
     */
    protected $rta;

    /**
     * @var \DateTime
     */
    protected $rtd;

    /**
     * @var bool
     */
    protected $preRegistration = false;

    /**
     * @var int
     */
    protected $id;

    /**
     * @var LanguageInterface
     */
    protected $language;

    /**
     * @var CustomerInterface
     */
    protected $customer;

    /**
     * @var SiteInterface
     */
    protected $site;

    /**
     * @var EntryPointInterface
     */
    protected $entryPoint;

    /**
     * @var HostInterface|null
     */
    protected $host;

    /**
     * @var VisitReasonInterface|null
     */
    protected $visitReason;

    /**
     * @var string|null
     */
    protected $licenseNumber;

    /**
     * @var string|null
     */
    protected $mobileNumber;

    /**
     * @var string|null
     */
    protected $salutation;

    /**
     * @var int
     */
    protected $customerUniqueId;

    /**
     * @var string
     */
    protected $occupation;

    /**
     * @var string
     */
    protected $status;

    /**
     * @var boolean
     */
    protected $hasLightPlan;

    /**
     * @var string
     */
    protected $referredBy;

    /**
     * @var boolean
     */
    protected $subscribedToNewsletter;

    /**
     * @var string
     */
    protected $uniqueId;

    /**
     * @var int
     */
    protected $numberOfPassengers;

    /**
     * @var int
     */
    protected $mtow;

    /**
     * @var float
     */
    protected $avgaz;

    /**
     * @var string
     */
    protected $arrivalFrom;

    /**
     * @var string
     */
    protected $departureTo;

    /**
     * Set companyName.
     *
     * @param string|null $companyName
     *
     * @return RegistrationInterface
     */
    public function setCompanyName($companyName)
    {
        $this->companyName = $companyName;

        return $this;
    }

    /**
     * Get companyName.
     *
     * @return string|null
     */
    public function getCompanyName()
    {
        return $this->companyName;
    }

    /**
     * Set firstname.
     *
     * @param string|null $firstname
     *
     * @return RegistrationInterface
     */
    public function setFirstname($firstname)
    {
        $this->firstname = $firstname;

        return $this;
    }

    /**
     * Get firstname.
     *
     * @return string|null
     */
    public function getFirstname()
    {
        return $this->firstname;
    }

    /**
     * Set lastname.
     *
     * @param string|null $lastname
     *
     * @return RegistrationInterface
     */
    public function setLastname($lastname)
    {
        $this->lastname = $lastname;

        return $this;
    }

    /**
     * Get full name (first + last name)
     *
     * @return string
     */
    public function getName()
    {
        return $this->getFirstname() . ' ' . $this->getLastname();
    }

    /**
     * Get lastname.
     *
     * @return string|null
     */
    public function getLastname()
    {
        return $this->lastname;
    }

    /**
     * Set email.
     *
     * @param string|null $email
     *
     * @return RegistrationInterface
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

    /**
     * Get email.
     *
     * @return string|null
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Set addressFirstLine.
     *
     * @param string|null $addressFirstLine
     *
     * @return RegistrationInterface
     */
    public function setAddressFirstLine($addressFirstLine)
    {
        $this->addressFirstLine = $addressFirstLine;

        return $this;
    }

    /**
     * Get addressFirstLine.
     *
     * @return string|null
     */
    public function getAddressFirstLine()
    {
        return $this->addressFirstLine;
    }

    /**
     * Set addressSecondLine.
     *
     * @param string|null $addressSecondLine
     *
     * @return RegistrationInterface
     */
    public function setAddressSecondLine($addressSecondLine)
    {
        $this->addressSecondLine = $addressSecondLine;

        return $this;
    }

    /**
     * Get addressSecondLine.
     *
     * @return string|null
     */
    public function getAddressSecondLine()
    {
        return $this->addressSecondLine;
    }

    /**
     * Set zipCode.
     *
     * @param string|null $zipCode
     *
     * @return RegistrationInterface
     */
    public function setZipCode($zipCode)
    {
        $this->zipCode = $zipCode;

        return $this;
    }

    /**
     * Get zipCode.
     *
     * @return string|null
     */
    public function getZipCode()
    {
        return $this->zipCode;
    }

    /**
     * Set city.
     *
     * @param string|null $city
     *
     * @return RegistrationInterface
     */
    public function setCity($city)
    {
        $this->city = $city;

        return $this;
    }

    /**
     * Get city.
     *
     * @return string|null
     */
    public function getCity()
    {
        return $this->city;
    }

    /**
     * Set gender.
     *
     * @param string|null $gender
     *
     * @return RegistrationInterface
     */
    public function setGender($gender)
    {
        $this->gender = $gender;

        return $this;
    }

    /**
     * Get gender.
     *
     * @return string|null
     */
    public function getGender()
    {
        return $this->gender;
    }

    /**
     * Set birthDate.
     *
     * @param \DateTime|null $birthDate
     *
     * @return RegistrationInterface
     */
    public function setBirthDate($birthDate)
    {
        $this->birthDate = $birthDate;

        return $this;
    }

    /**
     * Get birthDate.
     *
     * @return \DateTime|null
     */
    public function getBirthDate()
    {
        return $this->birthDate;
    }

    /**
     * Set country.
     *
     * @param string|null $country
     *
     * @return RegistrationInterface
     */
    public function setCountry($country)
    {
        $this->country = $country;

        return $this;
    }

    /**
     * Get country.
     *
     * @return string|null
     */
    public function getCountry()
    {
        return $this->country;
    }

    /**
     * Set website.
     *
     * @param string|null $website
     *
     * @return RegistrationInterface
     */
    public function setWebsite($website)
    {
        $this->website = $website;

        return $this;
    }

    /**
     * Get website.
     *
     * @return string|null
     */
    public function getWebsite()
    {
        return $this->website;
    }

    /**
     * Set workPermit.
     *
     * @param string|null $workPermit
     *
     * @return RegistrationInterface
     */
    public function setWorkPermit($workPermit)
    {
        $this->workPermit = $workPermit;

        return $this;
    }

    /**
     * @return string|null
     */
    public function getWorkPermit()
    {
        return $this->workPermit;
    }

    /**
     * @param $licensePlate
     *
     * @return RegistrationInterface
     */
    public function setLicensePlate($licensePlate)
    {
        $this->licensePlate = $licensePlate;

        return $this;
    }

    /**
     * @return string|null
     */
    public function getLicensePlate()
    {
        return $this->licensePlate;
    }

    /**
     * @param bool $incognito
     *
     * @return RegistrationInterface
     */
    public function setIncognito($incognito)
    {
        $this->incognito = $incognito;

        return $this;
    }

    /**
     * @return bool
     */
    public function getIncognito()
    {
        return $this->incognito;
    }

    /**
     * @param bool $refused
     *
     * @return RegistrationInterface
     */
    public function setRefused($refused)
    {
        $this->refused = $refused;

        return $this;
    }

    /**
     * @return bool
     */
    public function getRefused()
    {
        return $this->refused;
    }

    /**
     * Set eta.
     *
     * @param \DateTime $eta
     *
     * @return RegistrationInterface
     */
    public function setEta(\DateTime $eta)
    {
        $this->eta = $eta;

        return $this;
    }

    /**
     * Get eta.
     *
     * @return \DateTime
     */
    public function getEta()
    {
        return $this->eta;
    }

    /**
     * Set etd.
     *
     * @param \DateTime $etd
     *
     * @return RegistrationInterface
     */
    public function setEtd(?\DateTime $etd)
    {
        $this->etd = $etd;

        return $this;
    }

    /**
     * Get etd.
     *
     * @return \DateTime
     */
    public function getEtd()
    {
        return $this->etd;
    }

    /**
     * Set rta.
     *
     * @param \DateTime $rta
     *
     * @return RegistrationInterface
     */
    public function setRta(?\DateTime $rta)
    {
        $this->rta = $rta;

        return $this;
    }

    /**
     * Get rta.
     *
     * @return \DateTime
     */
    public function getRta()
    {
        return $this->rta;
    }

    /**
     * Set rtd.
     *
     * @param \DateTime $rtd
     *
     * @return RegistrationInterface
     */
    public function setRtd(\DateTime $rtd)
    {
        $this->rtd = $rtd;

        return $this;
    }

    /**
     * Get rtd.
     *
     * @return \DateTime
     */
    public function getRtd()
    {
        return $this->rtd;
    }

    /**
     * Set preRegistration.
     *
     * @param bool $preRegistration
     *
     * @return RegistrationInterface
     */
    public function setPreRegistration($preRegistration)
    {
        $this->preRegistration = $preRegistration;

        return $this;
    }

    /**
     * Get preRegistration.
     *
     * @return bool
     */
    public function getPreRegistration()
    {
        return $this->preRegistration;
    }

    /**
     * Get id.
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param LanguageInterface|null $language
     *
     * @return $this
     */
    public function setLanguage(LanguageInterface $language = null)
    {
        $this->language = $language;

        return $this;
    }

    /**
     * @return LanguageInterface
     */
    public function getLanguage()
    {
        return $this->language;
    }

    /**
     * @param CustomerInterface|null $customer
     *
     * @return $this
     */
    public function setCustomer(CustomerInterface $customer = null)
    {
        $this->customer = $customer;

        return $this;
    }

    /**
     * @return CustomerInterface
     */
    public function getCustomer()
    {
        return $this->customer;
    }

    /**
     * @param SiteInterface|null $site
     *
     * @return $this
     */
    public function setSite(SiteInterface $site = null)
    {
        $this->site = $site;

        if ($site) {
            $this->setCustomer($site->getCustomer());
        }

        return $this;
    }

    /**
     * @return SiteInterface
     */
    public function getSite()
    {
        return $this->site;
    }

    /**
     * @param EntryPointInterface|null $entryPoint
     *
     * @return $this
     */
    public function setEntryPoint(EntryPointInterface $entryPoint = null)
    {
        $this->entryPoint = $entryPoint;
        $this->setSite($entryPoint->getSite());

        return $this;
    }

    /**
     * @return EntryPointInterface
     */
    public function getEntryPoint()
    {
        return $this->entryPoint;
    }

    /**
     * @param HostInterface|null $host
     * @return $this
     */
    public function setHost(?HostInterface $host = null)
    {
        $this->host = $host;

        return $this;
    }

    /**
     * @return HostInterface|null
     */
    public function getHost()
    {
        return $this->host;
    }

    /**
     * @return string|null
     */
    public function getLicenseNumber()
    {
        return $this->licenseNumber;
    }

    /**
     * @param string|null $licenseNumber
     * @return RegistrationInterface
     */
    public function setLicenseNumber($licenseNumber)
    {
        $this->licenseNumber = $licenseNumber;

        return $this;
    }

    /**
     * @return string|null
     */
    public function getMobileNumber()
    {
        return $this->mobileNumber;
    }

    /**
     * @param string|null $mobileNumber
     * @return RegistrationInterface $this
     */
    public function setMobileNumber($mobileNumber)
    {
        $this->mobileNumber = $mobileNumber;
        return $this;
    }

    /**
     * @return VisitReasonInterface|null
     */
    public function getVisitReason()
    {
        return $this->visitReason;
    }

    /**
     * @param VisitReasonInterface|null $visitReason
     * @return RegistrationInterface $this
     */
    public function setVisitReason(VisitReasonInterface $visitReason = null)
    {
        $this->visitReason = $visitReason;
        return $this;
    }

    /**
     * Get the 2-letter country abbreviation
     *
     * @return string|null
     */
    public function getCountryCode()
    {
        return $this->countryCode;
    }

    /**
     * Set the 2-letter country abbreviation
     *
     * @param string|null $countryCode
     * @return Registration
     */
    public function setCountryCode($countryCode)
    {
        $this->countryCode = $countryCode;

        return $this;
    }

    /**
     * Get the full translated country name
     *
     * @return null|string
     */
    public function getAddressCountry()
    {
        return Intl::getRegionBundle()->getCountryName($this->countryCode);
    }

    /**
     * @param string|null $salutation
     * @return Registration
     */
    public function setSalutation($salutation)
    {
        $this->salutation = $salutation;

        return $this;
    }

    /**
     * @return string|null
     */
    public function getSalutation()
    {
        return $this->salutation;
    }

    /**
     * getUploadsFolder.
     *
     * @return string
     */
    public function getUploadsFolder()
    {
        return $this::UPLOADS_FOLDER;
    }

    /**
     * getAllowedFileExtensions.
     *
     * @return array
     */
    public function getAllowedFileExtensions()
    {
        return ['jpg', 'jpeg', 'png'];
    }

    /**
     * getAllowedFileMimeTypes.
     *
     * @return array
     */
    public function getAllowedFileMimeTypes()
    {
        return ['image/jpeg', 'image/pjpeg', 'image/png'];
    }

    /**
     * Get the gender name
     *
     * @return string
     */
    public function getGenderName()
    {
        $genders = array_flip(GenderType::toArray());

        return $genders[$this->gender] ?? '';
    }

    /**
     * @return int
     */
    public function getCustomerUniqueId()
    {
        return $this->customerUniqueId;
    }

    /**
     * @param int $customerUniqueId
     * @return RegistrationInterface
     */
    public function setCustomerUniqueId($customerUniqueId)
    {
        $this->customerUniqueId = $customerUniqueId;

        return $this;
    }

    /**
     * @return string
     */
    public function getOccupation()
    {
        return $this->occupation;
    }

    /**
     * @param string $occupation
     * @return RegistrationInterface
     */
    public function setOccupation($occupation)
    {
        $this->occupation = $occupation;

        return $this;
    }

    /**
     * @return string
     */
    public function getStatus()
    {
        return $this->status;
    }

    /**
     * @param string $status
     * @return RegistrationInterface
     */
    public function setStatus($status)
    {
        $this->status = $status;

        return $this;
    }

    /**
     * @return bool
     */
    public function hasLightPlan()
    {
        return $this->hasLightPlan;
    }

    /**
     * @param bool $hasLightPlan
     * @return RegistrationInterface
     */
    public function setHasLightPlan($hasLightPlan)
    {
        $this->hasLightPlan = $hasLightPlan;

        return $this;
    }

    /**
     * @return string
     */
    public function getReferredBy()
    {
        return $this->referredBy;
    }

    /**
     * @param string $referredBy
     * @return RegistrationInterface
     */
    public function setReferredBy($referredBy)
    {
        $this->referredBy = $referredBy;

        return $this;
    }

    /**
     * @return bool
     */
    public function isSubscribedToNewsletter()
    {
        return $this->subscribedToNewsletter;
    }

    /**
     * @param bool $subscribedToNewsletter
     * @return RegistrationInterface
     */
    public function setSubscribedToNewsletter($subscribedToNewsletter)
    {
        $this->subscribedToNewsletter = $subscribedToNewsletter;

        return $this;
    }

    /**
     * @return int
     */
    public function getNumberOfPassengers()
    {
        return $this->numberOfPassengers;
    }

    /**
     * @param int $numberOfPassengers
     * @return RegistrationInterface
     */
    public function setNumberOfPassengers($numberOfPassengers)
    {
        $this->numberOfPassengers = $numberOfPassengers;

        return $this;
    }

    /**
     * @return int
     */
    public function getMtow()
    {
        return $this->mtow;
    }

    /**
     * @param int $mtow
     * @return RegistrationInterface
     */
    public function setMtow($mtow)
    {
        $this->mtow = $mtow;

        return $this;
    }

    /**
     * @return float
     */
    public function getAvgaz()
    {
        return $this->avgaz;
    }

    /**
     * @param float $avgaz
     * @return RegistrationInterface
     */
    public function setAvgaz($avgaz)
    {
        $this->avgaz = $avgaz;

        return $this;
    }

    /**
     * @return string
     */
    public function getArrivalFrom()
    {
        return $this->arrivalFrom;
    }

    /**
     * @param string $arrivalFrom
     * @return RegistrationInterface
     */
    public function setArrivalFrom($arrivalFrom)
    {
        $this->arrivalFrom = $arrivalFrom;

        return $this;
    }

    /**
     * @return string
     */
    public function getDepartureTo()
    {
        return $this->departureTo;
    }

    /**
     * @param string $departureTo
     * @return RegistrationInterface
     */
    public function setDepartureTo($departureTo)
    {
        $this->departureTo = $departureTo;

        return $this;
    }

    /**
     * @return string
     */
    public function getUniqueId()
    {
        return $this->uniqueId ?? $this->getCustomerUniqueId();
    }

    /**
     * @param string $id
     * @return RegistrationInterface
     */
    public function setUniqueId($id)
    {
        $this->uniqueId = $id;

        return $this;
    }
}

在我的 RegistrationAdmin 类中,我拥有:

/**
 * @param DatagridMapper $datagridMapper
 */
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
    $datagridMapper
        ->add('companyName', null, [
            'label' => 'label.company_name',
        ])
        ->add('firstname', null, [
            'label' => 'label.firstname',
        ])
        ->add('lastname', null, [
            'label' => 'label.lastname',
        ])
        ->add('language', null, [
            'label' => 'label.language',
        ])
        ->add('allowedFrom', 'doctrine_orm_date', [
            'label' => 'label.allowed_from',
        ])
        ->add('visitReason', null, [
            'label' => 'label.visit_reason',
        ])
    ;
}

问题是访问原因(最后一个过滤器)在应用程序中显示了所有访问原因。

访问原因已链接(与客户客户相关)。并将客户链接(关联)到应用程序中的用户

因此,我想在自动完成下拉列表中显示与登录用户链接的所有访问原因。

但是我该怎么做?

1 个答案:

答案 0 :(得分:1)

查看http://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/filter_field_definition.html中的文档。添加新过滤器的参数包括字段名称,过滤器类型,过滤器配置,表单字段类型和表单字段配置。

//set visitReason from your user $visitsReasonUser
    ->add('my_filter', null, array(), 'entity', array(
              'class' => 'MyBundle:Registration',
              'query_builder' => function(EntityRepository $er) {
                   return $er->createQueryBuilder('u')
                             ->where('u.visitReason in :vistsUser')
                             ->setParameters(':visitUser',$visitsReasonUser');


               }
    ))