Created
July 12, 2012 12:08
-
-
Save merk/3097757 to your computer and use it in GitHub Desktop.
Symfony 2.1 collections with by_reference = false
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
<?php | |
namespace Ibms\CustomerBundle\Entity; | |
use Doctrine\Common\Collections\ArrayCollection; | |
use Doctrine\ORM\Mapping as ORM; | |
use Symfony\Component\Validator\Constraints as Assert; | |
/** | |
* @ORM\MappedSuperclass | |
*/ | |
abstract class Customer | |
{ | |
/** | |
* @ORM\Id | |
* @ORM\Column(type="integer") | |
* @ORM\GeneratedValue(strategy="AUTO") | |
* @var integer | |
*/ | |
protected $id; | |
/** | |
* @ORM\OneToMany(targetEntity="Ibms\CustomerBundle\Entity\CustomerContact", mappedBy="customer", orphanRemoval=true, cascade={"persist"}) | |
* @var \Doctrine\Common\Collections\Collection | |
*/ | |
protected $contacts; | |
public function __construct() | |
{ | |
$this->contacts = new ArrayCollection; | |
} | |
public function getContacts() | |
{ | |
return $this->contacts; | |
} | |
public function addContact(CustomerContact $contact) | |
{ | |
$this->contacts->add($contact); | |
$contact->setCustomer($this); | |
} | |
public function removeContact(CustomerContact $contact) | |
{ | |
$this->contacts->removeElement($contact); | |
$contact->setCustomer(null); | |
} | |
// ... | |
} |
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
<?php | |
namespace Ibms\CustomerBundle\Form\Type; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
class CustomerType extends AbstractType | |
{ | |
private $dataClass = 'Ibms\\CustomerBundle\\Entity\\Customer'; | |
public function __construct($dataClass) | |
{ | |
$this->dataClass = $dataClass; | |
} | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder->add('contacts', 'collection', array( | |
'type' => new CustomerContactType(), | |
'allow_add' => true, | |
'allow_delete' => true, | |
'prototype' => true, | |
'by_reference' => false, | |
)); | |
} | |
public function getDefaultOptions() | |
{ | |
return array('data_class' => $this->dataClass); | |
} | |
public function getName() | |
{ | |
return 'ibms_customer'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment