English 中文(简体)
书本
原标题:Symfony2 form collection
  • 时间:2012-05-18 07:07:44
  •  标签:
  • symfony

我有这个实体(Registro):

<?php

namespace GitekRegistroBundleEntity;

use DoctrineORMMapping as ORM;

/**
 * GitekRegistroBundleEntityRegistro
 *
 * @ORMTable()
 * @ORMEntity(repositoryClass="GitekRegistroBundleEntityRegistroRepository"))
 */
class Registro
{
    /**
     * @var integer $id
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;

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

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

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

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

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

    /** @ORMManyToOne(targetEntity="GitekUsuarioBundleEntityUsuario") */
    protected $usuario;

    /** @ORMManyToOne(targetEntity="GitekHotelBundleEntityTipotarea") */
    protected $tipotarea;

    /** @ORMManyToOne(targetEntity="GitekHotelBundleEntityHabitacion") */
    protected $habitacion;

    /** @ORMManyToOne(targetEntity="GitekRegistroBundleEntityMaster") */
    protected $master;

    /**
     * @ORMManyToMany(targetEntity="GitekHotelBundleEntityIncidencia", inversedBy="registros")
     * @ORMJoinTable(name="incidencia_registro",
     *      joinColumns={@ORMJoinColumn(name="registro_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORMJoinColumn(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 GitekUsuarioBundleEntityUsuario $usuario
     */
    public function setUsuario(GitekUsuarioBundleEntityUsuario $usuario)
    {
        $this->usuario = $usuario;
    }

    /**
     * Get usuario
     *
     * @return GitekUsuarioBundleEntityUsuario 
     */
    public function getUsuario()
    {
        return $this->usuario;
    }

    /**
     * Set tipotarea
     *
     * @param GitekHotelBundleEntityTipotarea $tipotarea
     */
    public function setTipotarea(GitekHotelBundleEntityTipotarea $tipotarea)
    {
        $this->tipotarea = $tipotarea;
    }

    /**
     * Get tipotarea
     *
     * @return GitekHotelBundleEntityTipotarea 
     */
    public function getTipotarea()
    {
        return $this->tipotarea;
    }

    /**
     * Set habitacion
     *
     * @param GitekHotelBundleEntityHabitacion $habitacion
     */
    public function setHabitacion(GitekHotelBundleEntityHabitacion $habitacion)
    {
        $this->habitacion = $habitacion;
    }

    /**
     * Get habitacion
     *
     * @return GitekHotelBundleEntityHabitacion 
     */
    public function getHabitacion()
    {
        return $this->habitacion;
    }

    /**
     * Add incidencias
     *
     * @param GitekHotelBundleEntityIncidencia $incidencias
     */
    public function addIncidencia(GitekHotelBundleEntityIncidencia $incidencias)
    {
        $this->incidencias[] = $incidencias;
    }

    /**
     * Get incidencias
     *
     * @return DoctrineCommonCollectionsCollection 
     */
    public function getIncidencias()
    {
        return $this->incidencias;
    }
}

我想一劳永逸地节省多份数据,因此我设立了一个新的实体,只有像现在这样一阵列:

<?php

namespace GitekRegistroBundleEntity;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMapping as ORM;

class Master
{

    /** @ORMOneToMany(targetEntity="GitekHotelBundleEntityHabitacion", 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;
    }
}

我创立了我的大通布。

<?php

namespace GitekRegistroBundleForm;

use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilder;
use DoctrineORMEntityRepository;

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  =>  GitekRegistroBundleEntityMaster 
        );
    }

    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 GitekRegistroBundleEntityMaster is not a valid entity or mapped super class.

I think that the problem is within de Master entity.

任何帮助或cl?

最佳回答

You missed the @Entity annotation on your Master class, you will also need a master table on your database for this to work.

如果你不希望创建主议席,那么你就能够跳出@Entity annotation,但你不能坚持主。 相反,你们必须通过收集工作来振兴,并且只坚持各阵列的实体。

问题回答

暂无回答




相关问题
Symfony 5, I can t start the server

I develop a symfony project and everything was fine. But since yesterday I can not launch the server: enter image description here The commands symfony server:start and symfony server:stop don t work ...

learning symfony 1.4 will be good when using symfony 2.0?

I know that the architecture is different in symfony 2.0 but im learning 1.4 right now. I wonder if this knowledge i gain about 1.4 will be usable for 2.0 in some extent or will it be a total waste ...

Is symfony 2.0 stable enough to use? [closed]

I wonder if Symfony 2.0 is stable enough to use? Because I ve never used Symfony before. It seems that Symfony 2 is much better than the previous version and I don t want to relearn/recode ...

your experience using symfony 2.0 [closed]

I m going to start a new project that s building web apps from scratch. I have been thinking about using symfony framework for this project. Should I start using symfony 2.0 or stick with 1.4 ? I ...

How to increase the session timeout in Symfony

I would like to know how to increase the session timeout in symfony. Is it enough to only adjust the symfony configuration settings or must I also configure anything in my php.ini file?

How stable or unstable is symfony 2.0? [closed]

Well, I know it s a preview, and I know it says that it s not yet ready for production, and yet I dare ask the question. I need to start building a pretty big application, which is planned to go live ...

热门标签