2012-05-18 58 views
2

我有这个实体(Registro):Symfony2的形式收集

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

我想保存在一排多个数据,所以我创建了一个新的实体,只有一个数组属性格式如下(主):

<?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() 
    )); 
} 

形式工作正常,和我看到的数据是正确的submited但不持久,I'm收到此错误所有的时间:

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

我觉得现在的问题是德内主实体。

任何帮助或线索?

回答

1

您错过了您的Master类的@Entity注释,您还需要在数据库上使用主表才能使其工作。

如果你不想创建主表,那么你可以跳过@Entity注释,但是你不能坚持主。相反,你将不得不遍历整个集合并只保存数组中的实体。

+0

邑,最后我用第一个选项,我有我的主表尽管是无用的。谢谢。 – ikerib