[英]How to combine two entities within one form?
I want to use two entities in one form and despite using tips from previous questions which were similar, it doesn't work.我想以一种形式使用两个实体,尽管使用了先前问题中类似的提示,但它不起作用。 I have following controller:
我有以下控制器:
<?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
<?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
<?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';
}
}
and finally twig template:最后是树枝模板:
{{ 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) }}
Why it doesn't work?为什么它不起作用? I receive following error:
我收到以下错误:
Neither the property "school" nor one of the methods "getSchool()", "school()", "isSchool()", "hasSchool()", "__get()" exist and have public access in class "AppBundle\Entity\User".
Your User class must have a propriety called school.你的 User 类必须有一个名为 school 的属性。
/**
* @ORM\Entity()
* @ORM\Table(name="users")
*/
class User
{
/**
* @ORM\OneToMany(targetEntity="Bundle\Entity\Schools",mappedBy="user")
*/
protected $school;
Then, generate entities for it,然后,为它生成实体,
php app/console doctrine:generate:entities Bundle:User
or write the functions manually:或手动编写函数:
public function setSchool($school)
{
$this->school= $school;
return $this;
}
public function getSchool()
{
return $this->school;
}
It's telling you exactly what you need to know.它准确地告诉您您需要知道的内容。 In your User entity, you either don't have a relation to your School entity or you are missing a getter & setter for School.
在您的 User 实体中,您要么与 School 实体没有关系,要么缺少 School 的 getter 和 setter。 Assuming it is a one to one relationship you should have something like:
假设它是一对一的关系,你应该有类似的东西:
/**
* @OneToOne(targetEntity="School", inversedBy="user")
* @JoinColumn(name="school_id", referencedColumnName="id")
**/
private $school;
With getter & setter:使用吸气剂和吸气剂:
public function setSchool(School $school = null)
{
$this->school = $school;
return $this;
}
public function getSchool()
{
return $this->school;
}
Note these should be automatically generated by the doctrine generate entities command .请注意,这些应该由学说生成实体命令自动生成。 You may simply not have run this if you have the User->School relationship set up correctly and the setter/getter missing.
如果您正确设置了 User->School 关系并且缺少 setter/getter,则您可能根本没有运行它。
Please also read the documentation on association mapping另请阅读有关关联映射的文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.