Last active
November 15, 2017 19:49
-
-
Save webdevilopers/1a01eb8c7a8290d0b951 to your computer and use it in GitHub Desktop.
Update One-To-Many relation using sonata_type_collection in Sonata Admin Bundle
This file contains 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
/** | |
* @ORM\Entity | |
* @ORM\Table(name="resident_log_translations") | |
*/ | |
class ResidentLogTranslation | |
{ | |
/** | |
* @ORM\Id | |
* @ORM\Column(type="integer") | |
* @ORM\GeneratedValue(strategy="AUTO") | |
*/ | |
private $id; | |
/** | |
* @ORM\ManyToOne(targetEntity="Plusquam\Bundle\ContractBundle\Entity\ResidentLog", inversedBy="translations") | |
* @ORM\JoinColumn(name="resident_log_id", referencedColumnName="id", nullable=false) | |
*/ | |
private $residentLog; | |
function getResidentLog() { | |
return $this->residentLog; | |
} | |
function setResidentLog($residentLog) { | |
$this->residentLog = $residentLog; | |
} | |
} |
This file contains 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
/** | |
* @ORM\Entity | |
* @ORM\Table(name="resident_logs") | |
*/ | |
class ResidentLog | |
{ | |
/** | |
* @ORM\Id | |
* @ORM\Column(type="integer") | |
* @ORM\GeneratedValue(strategy="AUTO") | |
*/ | |
private $id; | |
/** | |
* @ORM\OneToMany(targetEntity="ResidentLogTranslation", mappedBy="residentLog", cascade={"all"}, orphanRemoval=true)) | |
*/ | |
private $translations; | |
public function __construct() | |
{ | |
$this->translations = new ArrayCollection(); | |
} | |
/** | |
* Add translations | |
* | |
* @param ResidentLogTranslation $translation | |
* @return ResidentLog | |
*/ | |
public function addTranslation(ResidentLogTranslation $translation) | |
{ | |
$translation->setResidentLog($this); // !important | |
$this->translations[] = $translation; | |
return $this; | |
} | |
/** | |
* Remove translations | |
* | |
* @param ResidentLogTranslation $translations | |
*/ | |
public function removeTranslation(ResidentLogTranslation $translations) | |
{ | |
$this->translations->removeElement($translations); | |
} | |
/** | |
* Get translations | |
* | |
* @return \Doctrine\Common\Collections\Collection | |
*/ | |
public function getTranslations() | |
{ | |
return $this->translations; | |
} | |
function setTranslations($translations) { | |
$this->translations = $translations; | |
} | |
} |
This file contains 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
class ResidentLogAdmin extends Admin | |
{ | |
protected function configureFormFields(FormMapper $formMapper) | |
{ | |
$formMapper | |
->add('translations', 'sonata_type_collection', array( | |
'by_reference' => false // !important | |
), array( | |
'edit' => 'inline', | |
'inline' => 'table' | |
)) | |
; | |
} | |
} |
This file contains 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
class ResidentLogTranslationAdmin extends Admin | |
{ | |
protected $parentAssociationMapping = 'ResidentLog'; | |
protected function configureFormFields(FormMapper $formMapper) | |
{ | |
$formMapper | |
->add('language', 'sonata_type_model', array( | |
'btn_add' => false | |
)) | |
->add('text', 'textarea', array()) | |
; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The important part is setting
by_reference
to false as suggested by @webmozart here:symfony/symfony#1540 (comment)