Reputation: 172
There are two actions (new and edit) that use the same form. The form shall create a new entity (MyBundle\Entity\Project) or edit an existing one. Each project can be assigned to one team (MyBundle\Entity\Team), one team can hold multiple projects.
Entity/Project.php:
/* ... */
/**
* @var Team
*
* @ORM\ManyToOne(targetEntity="Team", inversedBy="projects")
* @ORM\JoinColumn(name="team", referencedColumnName="id")
*/
private $team;
/* ... */
Entity/Team.php:
/* ... */
/**
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="Project", mappedBy="team")
* @ORM\OrderBy({"title" = "ASC"})
**/
private $projects;
/* ... */
Controller/ProjectController.php:
public function newAction()
{
$project = new Project();
$form = $this->createForm(
new new ProjectType($project),
array(
'action' => $this->generateUrl('mybundle_project_create'),
)
);
/* ... */
}
public function editAction($id, Request $request)
{
/** @var \Doctrine\ORM\EntityManager $em */
$em = $this->getDoctrine()->getManager();
$project = $em->getRepository('MyBundle:Project')->findOneById($id);
$form = $this->createForm(
new ProjectType($project),
$project,
array(
'action' => $this->generateUrl('mybundle_project_edit', array('id' => $project->getId())),
)
);
Form/ProjectType.php:
private $project;
public function __construct($project = null)
{
$this->project = $project;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
/* ... */
->add('team', 'entity', array(
'class' => MyBundle:Team',
'property' => 'name',
'label' => 'Team',
'query_builder' => function (TeamRepository $er) {
return $er->createQueryBuilder('e')
->orderBy('e.name', 'ASC');
},
'expanded' => false,
'multiple' => false,
'required' => false
))
->add('save', 'submit');
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(
array(
'data_class' => 'MyBundle\Entity\Project',
)
);
}
/**
* @return string
*/
public function getName()
{
return 'mybundle_project';
}
When I'm accessing the edit form, the dropdown shows the assigned team correctly. But when I'm trying to create a new project, it gives me the following error message:
The form's view data is expected to be an instance of class MyBundle\Entity\Project, but is a(n) array. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) array to an instance of MyBundle\Entity\Project.
Upvotes: 0
Views: 717
Reputation: 141
ProjectController's newAction is calling the method createForm
with 2 parameters instead of 3, it should be
$form = $this->createForm(
new ProjectType($project),
$project,
array(
'action' => $this->generateUrl('mybundle_project_create'),
)
);
instead of
$form = $this->createForm(
new ProjectType($this->getDoctrine()->getEntityManager(), $project),
array(
'action' => $this->generateUrl('mybundle_project_create'),
)
);
Upvotes: 1