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

时间:2015-11-01 22:37:35

标签: php symfony

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

<?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';
    }
}

最后是twig模板:

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

2 个答案:

答案 0 :(得分:0)

您的用户类必须具有名为school的专属。

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

答案 1 :(得分:0)

它告诉你你究竟需要知道什么。在您的用户实体中,您要么与您的学校实体没有关系,要么您错过了一个getter&amp;学校的二传手。假设它是一对一的关系,你应该有类似的东西:

button:disabled {
    color:#717782;
}

getter&amp;设定器:

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

请注意,这些应由doctrine generate entities command自动生成。如果你正确设置了User-&gt; School关系并且缺少setter / getter,你可能根本就没有运行它。

另请阅读documentation on association mapping