@hhs: aqui te dejo las entitades y los formularios que estoy utilizando. Te los muestro solo con los atributos necesarios.
Entidades
Código PHP:
Ver original<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints as DoctrineAssert;
use Symfony\Component\Validator\Constraints
as Assert;
/**
* User
*
* @ORM\Table(name="user")
* @ORM\Entity(repositoryClass="AppBundle\Entity\UserRepository")
*/
class User
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var Phones
*
* @ORM\OneToMany(targetEntity="Phone", mappedBy="user", cascade={"persist" , "remove"})
*/
private $phones;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Constructor
*/
public function __construct()
{
$this->phones = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add phones
*
* @param \AppBundle\Entity\Phone $phones
* @return User
*/
public function addPhone(\AppBundle\Entity\Phone $phone)
{
$phone->setUser($this);
$this->phones->add($phone);
}
/**
* Remove phones
*
* @param \AppBundle\Entity\Phone $phones
*/
public function removePhone(\AppBundle\Entity\Phone $phone)
{
$this->phones->removeElement($phone);
}
/**
* Get phones
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getPhones()
{
return $this->phones;
}
public function setPhones(\Doctrine\Common\Collections\Collection $phones)
{
$this->phones = $phones;
foreach ($phones as $phone) {
$phone->setUser($this);
}
}
}
Código PHP:
Ver original<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints
as Assert; use Symfony\Bridge\Doctrine\Validator\Constraints as DoctrineAssert;
/**
* Phone
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="AppBundle\Entity\PhoneRepository")
*/
class Phone
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="number", type="string", length=20)
* @Assert\Regex(pattern="/^\+53\ ?[0-9]{8}$/", message = "field.phone")
*/
private $number;
/**
* @var \User
*
* @ORM\ManyToOne(targetEntity="User", inversedBy="phones", cascade={"persist"})
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="user", referencedColumnName="id", onDelete="Cascade", nullable=false)
* })
*/
private $user;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set number
*
* @param string $number
* @return Phone
*/
public function setNumber($number)
{
$this->number = $number;
return $this;
}
/**
* Get number
*
* @return string
*/
public function getNumber()
{
return $this->number;
}
/**
* Set user
*
* @param \AppBundle\Entity\User $user
* @return Phone
*/
public function setUser(\AppBundle\Entity\User $user)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return \AppBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
public function __toString() {
return $this->number;
}
}
Formularios
Código PHP:
Ver original<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints\Regex;
use Symfony\Component\Validator\Constraints\NotBlank;
class PhoneType extends AbstractType {
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm
(FormBuilderInterface
$builder, array $options) { $builder
->add('number', 'text', array( 'max_length' => 15,
'required' => false,
'label' => null,
'pattern' => "/^\+53\ ?[0-9]{8}$/",
'message' => 'field.phone'
)),
new NotBlank()
)
))
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array( 'data_class' => 'AppBundle\Entity\Phone'
));
}
/**
* @return string
*/
public function getName() {
return 'appbundle_phone';
}
}
Código PHP:
Ver original<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Length;
use AppBundle\Form\PhoneType;
class UserType extends AbstractType {
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm
(FormBuilderInterface
$builder, array $options) { $builder
->add('phones', 'collection', array( 'label' => false,
'type' => new PhoneType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'cascade_validation' => true,
))
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array( 'data_class' => 'AppBundle\Entity\User'
));
}
/**
* @return string
*/
public function getName() {
return 'appbundle_user';
}
}