Сущность User
<?php
namespace TestBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* User
*
* @ORM\Table(name="user")
* @ORM\Entity(repositoryClass="TestBundle\Repository\UserRepository")
*/
class User
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="email", type="string", length=255)
*/
private $email;
/**
* @ORM\OneToMany(targetEntity="Company", mappedBy="user")
*/
private $companies;
public function __construct()
{
$this->companies = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return User
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set email
*
* @param string $email
* @return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Add companies
*
* @param \TestBundle\Entity\Company $companies
* @return User
*/
public function addCompany(\TestBundle\Entity\Company $companies)
{
$this->companies[] = $companies;
return $this;
}
/**
* Remove companies
*
* @param \TestBundle\Entity\Company $companies
*/
public function removeCompany(\TestBundle\Entity\Company $companies)
{
$this->companies->removeElement($companies);
}
/**
* Get companies
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getCompanies()
{
return $this->companies;
}
}
Сущность Company
<?php
namespace TestBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Company
*
* @ORM\Table(name="company")
* @ORM\Entity(repositoryClass="TestBundle\Repository\CompanyRepository")
*/
class Company
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="company_name", type="string", length=255)
*/
private $companyName;
/**
* @var \DateTime
*
* @ORM\Column(name="year_finished", type="datetime")
*/
private $yearFinished;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="companies")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set companyName
*
* @param string $companyName
* @return Company
*/
public function setCompanyName($companyName)
{
$this->companyName = $companyName;
return $this;
}
/**
* Get companyName
*
* @return string
*/
public function getCompanyName()
{
return $this->companyName;
}
/**
* Set yearFinished
*
* @param \DateTime $yearFinished
* @return Company
*/
public function setYearFinished($yearFinished)
{
$this->yearFinished = $yearFinished;
return $this;
}
/**
* Get yearFinished
*
* @return \DateTime
*/
public function getYearFinished()
{
return $this->yearFinished;
}
/**
* Set user
*
* @param \TestBundle\Entity\User $user
* @return Company
*/
public function setUser(\TestBundle\Entity\User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return \TestBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
}
Формбилдеры соответственно
<?php
namespace TestBundle\Form\Type;
use TestBundle\Entity\Company;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name');
$builder->add('email');
$builder->add('companies', 'collection', array(
'type' => new CompanyType(),
'allow_add' => true,
// 'delete_empty' => true,
'prototype' => true,
// 'allow_delete' => true,
'by_reference' => false,
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'TestBundle\Entity\User',
));
}
/**
* @return string
*/
public function getName()
{
return 'resumeform';
}
}
<?php
namespace TestBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class CompanyType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('companyName');
$builder->add('yearFinished');
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'TestBundle\Entity\Company',
));
}
/**
* @return string
*/
public function getName()
{
return 'namecompany';
}
}
Контроллер:
<?php
namespace TestBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use TestBundle\Form\Type\UserType;
use TestBundle\Entity\User;
use TestBundle\Entity\Company;
class DefaultController extends Controller
{
/**
* @Route("/")
*/
public function indexAction(Request $request)
{
$user = new User();
$form = $this->createForm(new UserType(), $user);
return $this->render('TestBundle:Default:index.html.twig', array(
'form' => $form->createView(),
));
}
}
Шаблон
{{ form_start(form) }}
{{ form_widget(form) }}
<input type="submit">
{{ form_end(form) }}