我想在一種形式中使用兩個實體,儘管使用了以前類似的問題的提示,但它不起作用。我有以下控制器:如何在一個表單中組合兩個實體?
<?php
namespace AppBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use AppBundle\Entity\User;
use AppBundle\Form\UserType;
use AppBundle\Entity\School;
use AppBundle\Form\SchoolType;
class RegistrationController extends Controller
{
/**
* @Route("/register", name="user_register")
*/
public function registerAction(Request $request)
{
$user = new User();
$form = $this->createForm(new UserType(), $user);
$form->add('status','hidden', array('attr' => array('value' => '0')))
->add('school', new SchoolType());
return $this->render('AppBundle:Main:register.html.twig', array('form' => $form->createView()));
}
}
UserType.php
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList;
class UserType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('login', 'text', array('attr' => array(
'id' => 'login', 'name' => 'login', 'label' => 'Login:',
'class' => 'form-control', 'required' => true
)))
->add('password', 'password', array('attr' => array(
'id' => 'password', 'name' => 'password', 'label' => 'Password:',
'class' => 'form-control', 'required' => 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';
}
}
SchoolType.php
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class SchoolType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text', array('attr' => array(
'class' => 'form-control', 'id' => 'schoolName', 'name' => 'schoolName',
'placeholder' => 'np. Szkoła podstawowa nr. 5 w Warszawie', 'label' => 'Nazwa Szkoły',
'required' => true
)))
->add('shortcut', 'text', array('attr' => array(
'class' => 'form-control', 'id' => 'shortcut', 'name' => 'shortcut',
'placeholder' => 'np. SP5Warszawa', 'label' => 'Skrót', 'required' => true
)))
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\School'
));
}
/**
* @return string
*/
public function getName()
{
return 'school';
}
}
終於小枝模板:
{{ form_start(form) }}
<div class="form-group">
{{ form_row('form.school.name') }}
</div>
<div class="form-group">
{{ form_row('form.school.shortcut') }}
</div>
<div class="form-group">
{{ form_row('form.login') }}
</div>
<div class="form-group">
{{ form_row('form.login') }}
</div>
{{ form_row('form.status') }}
<span style="display:inline-block;">
<button type="submit" class="btn btn-default">Create</button>
{{ form_end(form) }}
爲什麼它不工作?我收到以下錯誤:
Neither the property "school" nor one of the methods "getSchool()", "school()", "isSchool()", "hasSchool()", "__get()" exist and have public access in class "AppBundle\Entity\User".