Skip to content

Instantly share code, notes, and snippets.

@webdevilopers
Last active November 15, 2017 19:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webdevilopers/1a01eb8c7a8290d0b951 to your computer and use it in GitHub Desktop.
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
/**
* @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;
}
}
/**
* @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;
}
}
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'
))
;
}
}
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())
;
}
}
@webdevilopers
Copy link
Author

The important part is setting by_reference to false as suggested by @webmozart here:
symfony/symfony#1540 (comment)


Probably the default value of "by_reference" had better been false (to avoid these headaches), but this would be a non-BC change now.```

Don't forget to add the `$child->setParent($this)` method on the `addChild()` method.

Related issues:
- https://github.com/sonata-project/SonataAdminBundle/issues/710
- https://github.com/sonata-project/SonataAdminBundle/issues/165

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment