如何在一个表单中组合两个实体?

如何在一个表单中组合两个实体?

问题描述:

我想在一种形式中使用两个实体,尽管使用了以前类似的问题的提示,但它不起作用。我有以下控制器:如何在一个表单中组合两个实体?

<?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". 

您的用户类必须具有称为学校的权限。

/** 
* @ORM\Entity() 
* @ORM\Table(name="users") 
*/ 
class User 
{ 

/** 
* @ORM\OneToMany(targetEntity="Bundle\Entity\Schools",mappedBy="user") 
*/ 
protected $school; 

然后,生成实体吧,

php app/console doctrine:generate:entities Bundle:User 

或手动编写功能:

public function setSchool($school) 
{ 
    $this->school= $school; 

    return $this; 
}  

public function getSchool() 
{ 
    return $this->school; 
} 

它告诉你你需要知道到底是什么。在您的用户实体中,您与您的学校实体没有任何关系,或者您错过了学校的获得者。假设它是一对一的关系,你应该有这样的:

/** 
    * @OneToOne(targetEntity="School", inversedBy="user") 
    * @JoinColumn(name="school_id", referencedColumnName="id") 
    **/ 
    private $school; 

吸气剂&二传手:

public function setSchool(School $school = null) 
{ 
    $this->school = $school; 

    return $this; 
} 

public function getSchool() 
{ 
    return $this->school; 
} 

注意这些应该由doctrine generate entities command自动生成。如果您的用户 - >学校关系设置正确并且缺少setter/getter,您可能根本就没有运行过。

请同时阅读documentation on association mapping