Symfony2表单集合

时间:2012-05-18 07:07:44

标签: symfony

我有这个实体(Registro):

<?php

namespace Gitek\RegistroBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Gitek\RegistroBundle\Entity\Registro
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Gitek\RegistroBundle\Entity\RegistroRepository"))
 */
class Registro
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var datetime $fecha
     *
     * @ORM\Column(name="fecha", type="datetime")
     */
    private $fecha;

    /**
     * @var smallint $comenzado
     *
     * @ORM\Column(name="comenzado", type="smallint", nullable=true)
     */
    private $comenzado;

    /**
     * @var smallint $completado
     *
     * @ORM\Column(name="completado", type="smallint", nullable=true)
     */
    private $completado;

   /**
     * @var datetime $created_at
     *
     * @ORM\Column(name="created_at", type="datetime")
     */
    private $created_at;

    /**
     * @var datetime $updated_at
     *
     * @ORM\Column(name="updated_at", type="datetime")
     */
    private $updated_at;

    /** @ORM\ManyToOne(targetEntity="Gitek\UsuarioBundle\Entity\Usuario") */
    protected $usuario;

    /** @ORM\ManyToOne(targetEntity="Gitek\HotelBundle\Entity\Tipotarea") */
    protected $tipotarea;

    /** @ORM\ManyToOne(targetEntity="Gitek\HotelBundle\Entity\Habitacion") */
    protected $habitacion;

    /** @ORM\ManyToOne(targetEntity="Gitek\RegistroBundle\Entity\Master") */
    protected $master;

    /**
     * @ORM\ManyToMany(targetEntity="Gitek\HotelBundle\Entity\Incidencia", inversedBy="registros")
     * @ORM\JoinTable(name="incidencia_registro",
     *      joinColumns={@ORM\JoinColumn(name="registro_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="incidencia_id", referencedColumnName="id")}
     * )
     */
    protected $incidencias;

    public function __construct()
    {
        $this->created_at = new \DateTime();
        $this->updated_at = new \DateTime();
    }

    // public function __toString()
    // {
    //     return $this->getNombre();
    // }




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

    /**
     * Set fecha
     *
     * @param datetime $fecha
     */
    public function setFecha($fecha)
    {
        $this->fecha = $fecha;
    }

    /**
     * Get fecha
     *
     * @return datetime 
     */
    public function getFecha()
    {
        return $this->fecha;
    }

    /**
     * Set comenzado
     *
     * @param smallint $comenzado
     */
    public function setComenzado($comenzado)
    {
        $this->comenzado = $comenzado;
    }

    /**
     * Get comenzado
     *
     * @return smallint 
     */
    public function getComenzado()
    {
        return $this->comenzado;
    }

    /**
     * Set completado
     *
     * @param smallint $completado
     */
    public function setCompletado($completado)
    {
        $this->completado = $completado;
    }

    /**
     * Get completado
     *
     * @return smallint 
     */
    public function getCompletado()
    {
        return $this->completado;
    }

    /**
     * Set created_at
     *
     * @param datetime $createdAt
     */
    public function setCreatedAt($createdAt)
    {
        $this->created_at = $createdAt;
    }

    /**
     * Get created_at
     *
     * @return datetime 
     */
    public function getCreatedAt()
    {
        return $this->created_at;
    }

    /**
     * Set updated_at
     *
     * @param datetime $updatedAt
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updated_at = $updatedAt;
    }

    /**
     * Get updated_at
     *
     * @return datetime 
     */
    public function getUpdatedAt()
    {
        return $this->updated_at;
    }

    /**
     * Set usuario
     *
     * @param Gitek\UsuarioBundle\Entity\Usuario $usuario
     */
    public function setUsuario(\Gitek\UsuarioBundle\Entity\Usuario $usuario)
    {
        $this->usuario = $usuario;
    }

    /**
     * Get usuario
     *
     * @return Gitek\UsuarioBundle\Entity\Usuario 
     */
    public function getUsuario()
    {
        return $this->usuario;
    }

    /**
     * Set tipotarea
     *
     * @param Gitek\HotelBundle\Entity\Tipotarea $tipotarea
     */
    public function setTipotarea(\Gitek\HotelBundle\Entity\Tipotarea $tipotarea)
    {
        $this->tipotarea = $tipotarea;
    }

    /**
     * Get tipotarea
     *
     * @return Gitek\HotelBundle\Entity\Tipotarea 
     */
    public function getTipotarea()
    {
        return $this->tipotarea;
    }

    /**
     * Set habitacion
     *
     * @param Gitek\HotelBundle\Entity\Habitacion $habitacion
     */
    public function setHabitacion(\Gitek\HotelBundle\Entity\Habitacion $habitacion)
    {
        $this->habitacion = $habitacion;
    }

    /**
     * Get habitacion
     *
     * @return Gitek\HotelBundle\Entity\Habitacion 
     */
    public function getHabitacion()
    {
        return $this->habitacion;
    }

    /**
     * Add incidencias
     *
     * @param Gitek\HotelBundle\Entity\Incidencia $incidencias
     */
    public function addIncidencia(\Gitek\HotelBundle\Entity\Incidencia $incidencias)
    {
        $this->incidencias[] = $incidencias;
    }

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

我想在一行中保存多个数据,所以我创建了一个新的实体,只有像这样的阵列(Master):

<?php

namespace Gitek\RegistroBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

class Master
{

    /** @ORM\OneToMany(targetEntity="Gitek\HotelBundle\Entity\Habitacion", mappedBy="master") */
    protected $registros;

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

    public function getRegistros()
    {
        return $this->registros;
    }

    public function setRegistros(ArrayCollection $registros)
    {
        $this->registros = $registros;
    }
}

我创建了我的MasterType:

<?php

namespace Gitek\RegistroBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Doctrine\ORM\EntityRepository;

class MasterType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {

        $builder
        ->add('registros', 'collection', array(
                'type' => new RegistroType(),
                'allow_add' => true,
                'by_reference' => true,
            ));
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'Gitek\RegistroBundle\Entity\Master'
        );
    }

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

这是我的控制者:

public function asignarAction()
{
    $master = new Master();
    $request = $this->getRequest();
    $form   = $this->createForm(new MasterType(), $master);

    if ('POST' === $request->getMethod()) {
        $form->bindRequest($request);

        if ($form->isValid()) {

            $em = $this->getDoctrine()->getEntityManager();
            $em->persist($master);
            $em->flush();

            return $this->redirect($this->generateUrl('recepcion_asignar'));
        }else {
            print_r("ezez");
            print_r($form->getErrors());
        }
    } else {

    }
    return $this->render('RegistroBundle:Recepcion:asignar.html.twig', array(
        'registro'  => $master,
        'form'      => $form->createView()
    ));
}

表单工作正常,我看到数据已正确提交,但没有保留,我一直收到此错误:

Class Gitek\RegistroBundle\Entity\Master is not a valid entity or mapped super class.

我认为问题出在de Master实体内。

任何帮助或线索?

1 个答案:

答案 0 :(得分:1)

您错过了Master类的@Entity注释,您还需要在数据库上使用主表来实现此目的。

如果您不想创建主表,则可以跳过@Entity注释,但不能保留master。相反,您必须遍历集合并仅保留数组中的实体。