带有实体的Symfony2表单总是将对象设置为null

时间:2015-11-02 16:32:28

标签: forms symfony entity

我正在尝试学习symfony2,但现在我通过使用带有实体的表单来对抗墙壁。 enitities将以(在列表中)的形式呈现,但是当我尝试检查哪个元素是selecet对象(这里是$ location)时它是空的(null)。 我尝试了手册中的任务样本没有任何问题,但在这里我丢失了:(。而且代码看起来几乎相同????

我的错误在哪里? 非常感谢。 问候 马丁

我的实体

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use AppBundle\Entity;


/**
 * Location
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Location
{

    /**
    * @ORM\OneToMany(targetEntity="Employee", mappedBy="location")
    */

    protected $employees;
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="street", type="string", length=255)
     */
    private $street;

    /**
     * @var string
     *
     * @ORM\Column(name="no", type="string", length=255)
     */
    private $no;

    /**
     * @var string
     *`enter code here`
     * @ORM\Column(name="city", type="string", length=255)
     */
    private $city;

    /**
     * @var string
     *
     * @ORM\Column(name="zip", type="string", length=255)
     */
    private $zip;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;


    public function __construct() {
        $this->employees=new ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set street
     *
     * @param string $street
     *
     * @return Location
     */
    public function setStreet($street)
    {
        $this->street = $street;

        return $this;
    }

    /**
     * Get street
     *
     * @return string
     */
    public function getStreet()
    {
        return $this->street;
    }

    /**
     * Set no
     *
     * @param string $no
     *
     * @return Location
     */
    public function setNo($no)
    {
        $this->no = $no;

        return $this;
    }

    /**
     * Get no
     *
     * @return string
     */
    public function getNo()
    {
        return $this->no;
    }

    /**
     * Set city
     *
     * @param string $city
     *
     * @return Location
     */
    public function setCity($city)
    {
        $this->city = $city;

        return $this;
    }

    /**
     * Get city
     *
     * @return string
     */
    public function getCity()
    {
        return $this->city;
    }

    /**
     * Set zip
     *
     * @param string $zip
     *
     * @return Location
     */
    public function setZip($zip)
    {
        $this->zip = $zip;

        return $this;
    }

    /**
     * Get zip
     *
     * @return string
     */
    public function getZip()
    {
        return $this->zip;
    }

    /**
     * Set name
     *
     * @param string $name
     *
     * @return Location
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Add employee
     *
     * @param \AppBundle\Entity\Employee $employee
     *
     * @return Location
     */
    public function addEmployee(\AppBundle\Entity\Employee $employee)
    {
        $this->employees[] = $employee;

        return $this;
    }

    /**
     * Remove employee
     *
     * @param \AppBundle\Entity\Employee $employee
     */
    public function removeEmployee(\AppBundle\Entity\Employee $employee)
    {
        $this->employees->removeElement($employee);
    }

    /**
     * Get employees
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getEmployees()
    {
        return $this->employees;
    }
}

我的表格类

<?php
/**
 * Description of LocationSelectType
 *
 * @author mwesterm
 */

namespace AppBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use AppBundle\Entity\Location;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\Extension\Core\ChoiceList\ObjectChoiceList;

class LocationSelectType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {


        $builder->add('name', 'entity', array(
                    'choice_label' => 'Name',
                    'class' => 'AppBundle\Entity\Location',
                    'required' => 'false',
                    'expanded' => 'true'
                ))
                ->add('new', 'submit', array('label' => 'Neu'))
                ->add('edit', 'submit', array('label' => 'Ändern'))
                ->add('delete', 'submit', array('label' => 'Löschen'));
    }

    public function getName() {
        return 'app_locationSelect';
    }

    public function configureOptions(OptionsResolver $resolver) {
        /* define your defaults here */
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Location'
        ));
    }
}

我的控制员:

<?php


namespace AppBundle\Controller\MasterData;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use AppBundle\Entity\Location;
use AppBundle\Form\Type\LocationSelectType;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;

class EditLocationData extends Controller {

    /**
     * @Route("editLocations", name="edit_locations")
     */
    public function EditLocationAction(Request $request) {

        $location = new Location();
        $form = $this->createForm(new LocationSelectType(), $location);
        $form->handleRequest($request);
        if ($form->isValid()) {
            if ($form->get('edit')->isClicked()) {
                return new Response('<html><body>Edit   ID:' . $location->getId() . ' Name;' . $location->getCity());
            }
        }
        return $this->render("masterData/locationEditSelect.html.twig", array(
                    'form' => $form->createView()
        ));
    }
}

1 个答案:

答案 0 :(得分:0)

会发生的是,您调用函数EditLocationAction的每个类型的变量$ location都将被重新实例化。

我认为,一旦你点击'#34;提交&#34;变量$ location将使用您选择的任何对象进行实例化。

变量$ request将带来您从视图中选择的任何信息,并通过handleRequest处理它以填充新的$ form变量。

(有关handleRequest的更多信息) http://api.symfony.com/2.4/Symfony/Component/Form/Form.html#method_handleRequest

对于快速修复(不知道是否是最佳实践),我可能会对变量$ request进行快速转储,并找出该数组中您要查找的内容。