Skip to content

Instantly share code, notes, and snippets.

@chalasr
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chalasr/0658a02b1c04180f5563 to your computer and use it in GitHub Desktop.
Save chalasr/0658a02b1c04180f5563 to your computer and use it in GitHub Desktop.
How make an auto-completed field for filter entity results in list view, using entity property (not related entity) ?
<?php
namespace Application\PersonneBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Personne
*
* @ORM\Table()
* @ORM\HasLifecycleCallbacks
* @ORM\Entity(repositoryClass="Application\PersonneBundle\Entity\PersonneRepository")
*/
class Personne
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
protected $name;
/**
* @var string
*
* @ORM\Column(name="firstname", type="string", length=255)
*/
protected $firstname;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=255)
*/
protected $title;
/**
* @var string
*
* @ORM\Column(name="society", type="string", length=255, nullable=true)
*/
protected $society;
/**
* @var string
*
* @ORM\Column(name="indication", type="string", length=255, nullable=true)
*/
protected $indication;
/**
* @var string
*
* @ORM\Column(name="adress1", type="string", length=255)
*/
protected $adress1;
/**
* @var string
*
* @ORM\Column(name="adress2", type="string", length=255, nullable=true)
*/
protected $adress2;
/**
* @var string
*
* @ORM\Column(name="city", type="string", length=255)
*/
protected $city;
/**
* @var string
*
* @ORM\Column(name="zipcode", type="string", length=255)
*/
protected $zipcode;
/**
* @var string
*
* @ORM\Column(name="country", type="string", length=255)
*/
protected $country;
/**
* @var \DateTime
*
* @ORM\Column(name="birthdate", type="date", nullable=true)
*/
protected $birthdate;
/**
* @var \Don
* @ORM\ManyToOne(targetEntity="PersonneOrigin", inversedBy="personnes")
* @ORM\JoinColumn(name="origin_id", referencedColumnName="id", nullable=true)
*/
protected $origin;
/**
* @var boolean
*
* @ORM\Column(name="journal", type="boolean", nullable=true)
*/
protected $journal;
/**
* @var boolean
*
* @ORM\Column(name="calendar", type="boolean", nullable=true)
*/
protected $calendar;
/**
* @var boolean
*
* @ORM\Column(name="receipt", type="boolean", nullable=true)
*/
protected $receipt;
/**
* @var boolean
*
* @ORM\Column(name="annual_receipt", type="boolean", nullable=true)
*/
protected $annualReceipt;
/**
* @var boolean
*
* @ORM\Column(name="dead", type="boolean", nullable=true)
*/
protected $dead;
/**
* @var boolean
*
* @ORM\Column(name="renting", type="boolean", nullable=true)
*/
protected $renting;
/**
* @var \Don
* @ORM\ManyToOne(targetEntity="PersonneAnswerMethod", inversedBy="personnes")
* @ORM\JoinColumn(name="answermethod_id", referencedColumnName="id", nullable=true)
*/
protected $answerBy;
/**
* @var string
*
* @ORM\Column(name="banking_informations", type="text", nullable=true)
*/
protected $bankingInformations;
/**
* @var string
*
* @ORM\Column(name="observation", type="text", nullable=true)
*/
protected $observation;
/**
* @var \DateTime
*
* @ORM\Column(name="created_at", type="date")
*/
protected $createdAt;
/**
* @ORM\OneToMany(targetEntity="EmailPersonne", mappedBy="personne", cascade={"remove","persist"}, orphanRemoval=true)
*/
protected $emails;
/**
* @ORM\OneToMany(targetEntity="PhonePersonne", mappedBy="personne", cascade={"remove","persist"}, orphanRemoval=true)
*/
protected $phones;
/**
* @ORM\OneToMany(targetEntity="Application\DonBundle\Entity\Don", mappedBy="personne", cascade={"remove",
* "persist"})
*/
protected $dons;
/**
* @var string
*
* @ORM\Column(name="lastRegisteredDon", type="date", nullable=true)
*/
protected $lastRegisteredDon;
public function setId($id)
{
$this->id = $id;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Personne
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set firstname
*
* @param string $firstname
* @return Personne
*/
public function setFirstname($firstname)
{
$this->firstname = $firstname;
return $this;
}
/**
* Get firstname
*
* @return string
*/
public function getFirstname()
{
return $this->firstname;
}
/**
* Set title
*
* @param string $title
* @return Personne
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set society
*
* @param string $society
* @return Personne
*/
public function setSociety($society)
{
$this->society = $society;
return $this;
}
/**
* Get society
*
* @return string
*/
public function getSociety()
{
return $this->society;
}
/**
* Set city
*
* @param string $city
* @return Personne
*/
public function setCity($city)
{
$this->city = $city;
return $this;
}
/**
* Get city
*
* @return string
*/
public function getCity()
{
return $this->city;
}
/**
* Set country
*
* @param string $country
* @return Personne
*/
public function setCountry($country)
{
$this->country = $country;
return $this;
}
/**
* Get country
*
* @return string
*/
public function getCountry()
{
return $this->country;
}
/**
* Set email
*
* @param string $email
* @return Personne
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set phone
*
* @param string $phone
* @return Personne
*/
public function setPhone($phone)
{
$this->phone = $phone;
return $this;
}
/**
* Get phone
*
* @return string
*/
public function getPhone()
{
return $this->phone;
}
/**
* Set birthdate
*
* @param \DateTime $birthdate
* @return Personne
*/
public function setBirthdate($birthdate)
{
$this->birthdate = $birthdate;
return $this;
}
/**
* Get birthdate
*
* @return \DateTime
*/
public function getBirthdate()
{
return $this->birthdate;
}
/**
* Set journal
*
* @param boolean $journal
* @return Personne
*/
public function setJournal($journal)
{
$this->journal = $journal;
return $this;
}
/**
* Get journal
*
* @return boolean
*/
public function getJournal()
{
return $this->journal;
}
/**
* Set calendar
*
* @param boolean $calendar
* @return Personne
*/
public function setCalendar($calendar)
{
$this->calendar = $calendar;
return $this;
}
/**
* Get calendar
*
* @return boolean
*/
public function getCalendar()
{
return $this->calendar;
}
/**
* Set receipt
*
* @param boolean $receipt
* @return Personne
*/
public function setReceipt($receipt)
{
$this->receipt = $receipt;
return $this;
}
/**
* Get receipt
*
* @return boolean
*/
public function getReceipt()
{
return $this->receipt;
}
/**
* Set annualReceipt
*
* @param boolean $annualReceipt
* @return Personne
*/
public function setAnnualReceipt($annualReceipt)
{
$this->annualReceipt = $annualReceipt;
return $this;
}
/**
* Get annualReceipt
*
* @return boolean
*/
public function getAnnualReceipt()
{
return $this->annualReceipt;
}
/**
* Set dead
*
* @param boolean $dead
* @return Personne
*/
public function setDead($dead)
{
$this->dead = $dead;
return $this;
}
/**
* Get dead
*
* @return boolean
*/
public function getDead()
{
return $this->dead;
}
/**
* Set renting
*
* @param boolean $renting
* @return Personne
*/
public function setRenting($renting)
{
$this->renting = $renting;
return $this;
}
/**
* Get renting
*
* @return boolean
*/
public function getRenting()
{
return $this->renting;
}
/**
* Set bankingInformations
*
* @param string $bankingInformations
* @return Personne
*/
public function setBankingInformations($bankingInformations)
{
$this->bankingInformations = $bankingInformations;
return $this;
}
/**
* Get bankingInformations
*
* @return string
*/
public function getBankingInformations()
{
return $this->bankingInformations;
}
/**
* Set observation
*
* @param string $observation
* @return Personne
*/
public function setObservation($observation)
{
$this->observation = $observation;
return $this;
}
/**
* Get observation
*
* @return string
*/
public function getObservation()
{
return $this->observation;
}
/**
* Constructor
*/
public function __construct()
{
$this->emails = new \Doctrine\Common\Collections\ArrayCollection();
$this->phones = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add emails
*
* @param \Application\PersonneBundle\Entity\EmailPersonne $emails
* @return Personne
*/
public function addEmail(\Application\PersonneBundle\Entity\EmailPersonne $emails)
{
$emails->setPersonne($this);
$this->emails[] = $emails;
return $this;
}
/**
* Remove emails
*
* @param \Application\PersonneBundle\Entity\EmailPersonne $emails
*/
public function removeEmail(\Application\PersonneBundle\Entity\EmailPersonne $emails)
{
$this->emails->removeElement($emails);
}
/**
* Get emails
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getEmails()
{
return $this->emails;
}
// __toString method needed for display relation entries
/**
* toString
*/
public function __toString(){
$fullNameId = $this->getFirstname() . ' ' . $this->getName() . ' - ' . $this->getId();
$fullNameId = $this->getId() ? $fullNameId : 'Nouvelle personne';
return $fullNameId;
}
/**
* Add phones
*
* @param \Application\PersonneBundle\Entity\PhonePersonne $phones
* @return Personne
*/
public function addPhone(\Application\PersonneBundle\Entity\PhonePersonne $phones)
{
$phones->setPersonne($this);
$this->phones[] = $phones;
return $this;
}
/**
* Remove phones
*
* @param \Application\PersonneBundle\Entity\PhonePersonne $phones
*/
public function removePhone(\Application\PersonneBundle\Entity\PhonePersonne $phones)
{
$this->phones->removeElement($phones);
}
/**
* Get phones
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getPhones()
{
return $this->phones;
}
/**
* Set indication
*
* @param string $indication
* @return Personne
*/
public function setIndication($indication)
{
$this->indication = $indication;
return $this;
}
/**
* Get indication
*
* @return string
*/
public function getIndication()
{
return $this->indication;
}
/**
* Set adress1
*
* @param string $adress1
* @return Personne
*/
public function setAdress1($adress1)
{
$this->adress1 = $adress1;
return $this;
}
/**
* Get adress1
*
* @return string
*/
public function getAdress1()
{
return $this->adress1;
}
/**
* Set adress2
*
* @param string $adress2
* @return Personne
*/
public function setAdress2($adress2)
{
$this->adress2 = $adress2;
return $this;
}
/**
* Get adress2
*
* @return string
*/
public function getAdress2()
{
return $this->adress2;
}
/**
* Set zipcode
*
* @param string $zipcode
* @return Personne
*/
public function setZipcode($zipcode)
{
$this->zipcode = $zipcode;
return $this;
}
/**
* Get zipcode
*
* @return string
*/
public function getZipcode()
{
return $this->zipcode;
}
/**
*
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function updatedTimestamps()
{
if($this->getCreatedAt() == null)
{
$this->setCreatedAt(new \DateTime(date('Y-m-d H:i:s')));
}
}
/**
* Set createdAt
*
* @param \DateTime $createdAt
* @return Personne
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Add dons
*
* @param \Application\DonBundle\Entity\Don $dons
* @return Personne
*/
public function addDon(\Application\DonBundle\Entity\Don $dons)
{
$this->dons[] = $dons;
return $this;
}
/**
* Remove dons
*
* @param \Application\DonBundle\Entity\Don $dons
*/
public function removeDon(\Application\DonBundle\Entity\Don $dons)
{
$this->dons->removeElement($dons);
}
/**
* Get dons
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getDons()
{
return $this->dons;
}
/**
* Set origin
*
* @param \Application\PersonneBundle\Entity\PersonneOrigin $origin
* @return Personne
*/
public function setOrigin(\Application\PersonneBundle\Entity\PersonneOrigin $origin = null)
{
$this->origin = $origin;
return $this;
}
/**
* Get origin
*
* @return \Application\PersonneBundle\Entity\PersonneOrigin
*/
public function getOrigin()
{
return $this->origin;
}
/**
* Set answerBy
*
* @param \Application\PersonneBundle\Entity\PersonneAnswerMethod $answerBy
* @return Personne
*/
public function setAnswerBy(\Application\PersonneBundle\Entity\PersonneAnswerMethod $answerBy = null)
{
$this->answerBy = $answerBy;
return $this;
}
/**
* Get answerBy
*
* @return \Application\PersonneBundle\Entity\PersonneAnswerMethod
*/
public function getAnswerBy()
{
return $this->answerBy;
}
/**
* Get lastRegisteredDon
*
* @return \DateTime
*/
public function getLastRegisteredDon()
{
return $this->lastRegisteredDon != null ? $this->lastRegisteredDon->format('d/m/Y') : $this->lastRegisteredDon;
}
/**
* Set lastRegisteredDon
*
* @param \DateTime $lastRegisteredDon
* @return Personne
*/
public function setLastRegisteredDon($lastRegisteredDon)
{
$this->lastRegisteredDon = $lastRegisteredDon;
return $this;
}
}
<?php
// src/Acme/DemoBundle/Admin/PostAdmin.php
namespace Application\PersonneBundle\Admin;
use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Application\PersonneBundle\Entity\Personne;
class PersonneAdmin extends Admin
{
// Fields to be shown on create/edit forms
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('Personne', array('class' => 'col-md-12 perDef'))
//If action != 'create', display Personne id
->add('id', $this->id($this->getSubject()) ? 'text' : 'hidden', array(
'label' => 'Numéro',
'required' => false,
'disabled' => true,
'read_only' => true,
'attr'=> array(
'class'=>'input-id input-sm',
'disabled' => true,
),
))
->add('dead', 'checkbox', array(
'label' => 'Décédé',
'attr'=> array('class'=>'col-md-2'),
'required' => false,
))
->end()
//Informations fieldset
->with('Informations', array('class' => 'col-md-6'))
->add('title', 'choice', array(
'label' => 'Civilité',
'choices' => array(
'MR' => 'MR',
'MME' => 'MME',
'MLLE' => 'MLLE',
'MR ET MME' => 'MR ET MME',
'MR L ABBE' => 'MR L ABBE',
'SOEUR' => 'SOEUR',
'MR LE DR' => 'MR LE DR',
'MAITRE' => 'MAITRE',
'MME VVE' => 'MME VVE',
'MGR' => 'MGR',
'PERE' => 'PERE',
'FRERE' => 'FRERE',
'MR LE DIRECTEUR' => 'MR LE DIRECTEUR',
'MMES' => 'MMES',
'MRS' => 'MRS',
'MLLES' => 'MLLES',
'SOEURS' => 'SOEURS',
'MME ET MLLE' => 'MME ET MLLE',
'MR LE CHANOINE' => 'MR LE CHANOINE',
'MR LE DR ET MME' => 'MR LE DR ET MME',
'MR ET MLLE' => 'MR ET MLLE',
'MME LE DR' => 'MME LE DR',
'FAMILLE' => 'FAMILLE',
'DR' => 'DR',
)
))
->add('name', 'text', array('label' => 'Nom'))
->add('firstname', 'text', array('label' => 'Prénom'))
->add('society', 'text', array(
'label' => 'Société / Association',
'required' => false,
))
->add('emails', 'sonata_type_collection', array(
'by_reference' => false,
'cascade_validation' => true,
'required' => false,
),array(
'edit' => 'inline',
'inline' => 'table',
'admin_code' => 'sonata.admin.personne_email'
))
->add('phones', 'sonata_type_collection', array(
'by_reference' => false,
'cascade_validation' => true,
'required' => false,
'label' => 'Téléphones'
),array(
'edit' => 'inline',
'inline' => 'table',
'admin_code' => 'sonata.admin.personne_phones'
))
->add('birthdate', 'sonata_type_date_picker', array(
'label' => 'Date de naissance',
'format'=>'dd/MM/yyyy',
'required' => false,
))
->end()//end fieldset
//address fieldset
->with('Coordonnées', array('class' => 'col-md-6'))
->add('indication', 'text', array(
'label' => 'Indication',
'required' => false,
))
->add('adress1', 'text', array('label' => 'Adresse 1'))
->add('adress2', 'text', array('label' => 'Adresse 2', 'required' => false))
->add('zipcode', 'text', array('label' => 'Code postal'))
->add('city', 'text', array('label' => 'Ville'))
->add('country', 'text', array('label' => 'Pays'))
->end()//end fieldset
//complement1 fielset
->with('Complément', array('class' => 'col-md-6 clearboth perCom'))
->add('origin', 'sonata_type_model', array(
'label' => $this->translator->trans('Provenance de la personne',array(),'don')
))
->add('journal', 'checkbox', array(
'label' => 'Abonné à la revue ',
'required' => false,
))
->add('calendar', 'checkbox', array(
'label' => 'Envoi calendrier ',
'required' => false,
))
->add('receipt', 'checkbox', array(
'label' => 'Envoi reçus fiscaux',
'required' => false,
))
->add('annualReceipt', 'checkbox', array(
'label' => 'Envoi reçu fiscal annuel',
'required' => false,
))
->add('renting', 'checkbox', array(
'label' => 'Louer ses données',
'required' => false,
))
->add('answerBy', 'sonata_type_model', array(
'label' => $this->translator->trans('Support réponse',array(),'don'),
'required' => true,
))
->end()//end fieldset
//complement2 fielset
->with('Prélèvement et Observations', array('class' => 'col-md-6'))
->add('bankingInformations', 'textarea', array(
'label' => 'Informations prélévement',
'required' => false,
))
->add('observation', 'textarea', array(
'label' => 'Observations',
'required' => false,
))
->end()//end fieldset
;
if ($this->id($this->getSubject())) {
$formMapper->add('lastRegisteredDon', 'hidden');
}
}
// Fields to be shown on filter forms
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add('id', null, array('label' => 'Numéro'))
->add('name', null, array('label' => 'Nom'))
->add('firstname', null, array('label' => 'Prénom'))
->add('society', null, array('label' => 'Société'))
->add('lastRegisteredDon', 'doctrine_orm_date', array('label' => 'Date du dernier don'), 'sonata_type_date_picker',array(
'format' => 'dd/MM/yyyy',
'widget' => 'single_text',
'attr' => array(
'class' => 'datepicker'
)
))
->add('zipcode', null, array('label' => 'Code Postal'))
->add('country', null, array('label' => 'Pays'))
->add('haveEmails', 'doctrine_orm_callback', array(
'label' => 'Emails renseigné',
'callback' => function($queryBuilder, $alias, $field, $value) {
if(!$value['value']) {
return;
}
if($value['value'] == 'yes') {
$queryBuilder
->leftJoin(sprintf('%s.emails', $alias), 'c')
->andWhere('c.email IS NOT NULL');
}elseif($value['value'] == 'no') {
$queryBuilder
->leftJoin(sprintf('%s.emails', $alias), 'c')
->andWhere('c.email IS NULL');
}
}), 'choice', array(
'choices' => array(
'' => '',
'yes' => 'Oui',
'no' => 'Non'
),
))
->add('birthdate', 'doctrine_orm_date', array('label' => 'Date de naissance'), 'sonata_type_date_picker',array(
'format' => 'dd/MM/yyyy',
'widget' => 'single_text',
'attr' => array(
'class' => 'datepicker newline'
)
))
->add('createdAt', 'doctrine_orm_date', array('label' => 'Date d\'inscription'), 'sonata_type_date_picker',array(
'format' => 'dd/MM/yyyy',
'widget' => 'single_text',
'attr' => array(
'class' => 'datepicker newline'
)
))
->add('journal', null, array('label' => 'Abonné revue'))
->add('calendar', null, array('label' => 'Abonné calendrier'))
// HERE THE WANTED FILTER
// I want filter results by current entity name property (not on a related entity)
// So this entity is named Personne , I want filter results on Personne.name using autocompletion
// How can I achieve this ?
//
->add('personne', 'doctrine_orm_model_autocomplete', array('label' => 'AutoComplete'), null, array(
'property' => 'name'
))
// error output : " The option `association_mapping` must be set for field: `personne` "
// END
;
}
// Fields to be shown on lists
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->add('id', null,array('label' => 'Numéro'))
->add('name','text',array('label' => 'Nom'))
->add('firstname','text',array('label' => 'Prénom'))
->add('emails',null,array('label' => 'Emails'))
->add('lastRegisteredDon',null,array('label' => 'Date du dernier don','format'=> 'm/d/Y',))
->add('zipcode','text',array('label' => 'Code postal'))
->add('country','text',array('label' => 'Pays'))
->add('birthdate','date',array(
'label' => "Date de naissance",
'format' => 'd/m/Y'
))
->add('createdAt','date',array(
'label' => 'Date inscription',
'format' => 'd/m/Y'
))
->add('journal',null,array('label' => 'Abonné à la revue'))
->add('calendar',null,array('label' => 'Abonné au calendrier'))
->add('total_dons', null,array(
'label' => 'Total des dons',
'template' => 'ApplicationPersonneBundle:PersonneAdmin:get_total_dons.html.twig'
))
->add('_action', 'actions', array(
'actions' => array(
'edit' => array(),
'delete' => array(),
'add_don' => array(
'template' => 'ApplicationPersonneBundle:PersonneAdmin:add_don_button.html.twig',
'label' => $this->translator->trans('Ajouter un don', array(), 'personne'),
),
)
))
;
}
public function getTemplate($name)
{
switch ($name) {
case 'edit':
return 'ApplicationPersonneBundle:PersonneAdmin:base_edit.html.twig';
break;
case 'list':
return 'ApplicationPersonneBundle:PersonneAdmin:list.html.twig';
break;
default:
return parent::getTemplate($name);
break;
}
}
public function preUpdate($personne)
{
$don = $personne->getDons()->last();
$date = $don ? $don->getRegisteredOn() : '';
$personne->setLastRegisteredDon($date);
}
public function getCreateLabel()
{
return $this->translator->trans('Créer une personne',array(),'don');
}
public function getTotalDons($personne_id)
{
$total = 0;
$container = $this->getConfigurationPool()->getContainer();
$personne = $container->get('doctrine')->getRepository('ApplicationPersonneBundle:Personne')
->find($personne_id);
$dons = $container->get('doctrine')->getRepository('ApplicationDonBundle:Don')
->findByPersonne($personne);
if(isset($dons[0])){
foreach ($dons as $don) {
$amount = $don->getTotal();
$total += $amount;
}
}
return $total;
}
}