Created
November 26, 2013 13:46
-
-
Save thesowah/7658460 to your computer and use it in GitHub Desktop.
Symfony2 1:M / 1:1 Relationship and Sonata Admin Form
Rerence: - https://github.com/sonata-project/SonataAdminBundle/issues/1254 - http://sonata-project.org/bundles/admin/master/doc/reference/form_types.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//... | |
class Person | |
{ | |
/* @var \Doctrine\Common\Collections\Collection */ | |
private $talents | |
/* @var string */ | |
private $gender | |
/* @var string */ | |
private $name | |
/* Constructor */ | |
public function __contruct() | |
{ | |
$this->talents = new \Doctrine\Common\Collections\ArrayCollection(); | |
} | |
/* Add talents | |
* @param \Company\MyBundle\Entity\Talent $talents */ | |
public function addTalent(\Company\MyBundle\Entity\Talent $talents) | |
{ | |
$talents->setPerson($this); | |
$this->talents->add($talents); | |
} | |
/* Set talents | |
* @param \Company\MyBundle\Entity\Talent $talents | |
* @return Person */ | |
public function setTalents(\Company\MyBundle\Entity\Talent $talents) | |
{ | |
$this->talents = new \Doctrine\Common\Collections\ArrayCollection(); | |
foreach($talents as $talent) | |
{ | |
$this->addTalent($talent); | |
} | |
return $this; | |
} | |
/* Remove farm | |
* @param \Company\MyBundle\Entity\Talent $talent */ | |
public function removeTalent(\Company\MyBundle\Entity\Talent $talent) | |
{ | |
$this->talents->removeElement($talent); | |
} | |
// ... | |
// setter & getter for $gender and $name | |
/* */ | |
public static function getGenderList() | |
{ | |
return array( | |
'male' => 'Male', | |
'female' => 'Female' | |
); | |
} | |
/* */ | |
public static function getGenderValues(){ | |
return array(self::getGenderList()); | |
} | |
/* @var string */ | |
public function __toString(){ | |
return $this->getName()?:''; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//... | |
class PersonAdmin extends Admin | |
{ | |
//... | |
/* @param FormMapper $formMapper */ | |
protected function configureFormFields(FormMapper $formMapper) | |
{ | |
$formMapper | |
->add('name') | |
->add('gender', 'choice', array( | |
'choices' => Person::getGenderList(), | |
//'expanded' => true, | |
)) | |
->add('talents', 'sonata_type_collection', array('required' => false), array( | |
'edit' => 'inline', | |
'inline' => 'table', | |
'sortable' => 'position', | |
)) | |
; | |
} | |
//... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment