2015-11-01 51 views
-1

我想在一種形式中使用兩個實體,儘管使用了以前類似的問題的提示,但它不起作用。我有以下控制器:如何在一個表單中組合兩個實體?

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

回答

0

您的用戶類必須具有稱爲學校的權限。

/** 
* @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; 
} 
0

它告訴你你需要知道到底是什麼。在您的用戶實體中,您與您的學校實體沒有任何關係,或者您錯過了學校的獲得者。假設它是一對一的關係,你應該有這樣的:

/** 
    * @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

相關問題