我如何将ArrayCollection持久化到mysql?

时间:2012-12-11 16:22:48

标签: collections symfony-2.1 persist

问题如下: 在谈论无线电我有节目时,每个节目都必须有广告,所以每个节目都有一个或多个广告提及。

Mi实体计划:

<?php

namespace MediterraneoFM\MediterraneoFMBundle\Entity;

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

/**
 * MediterraneoFM\MediterraneoFMBundle\Entity\Programas
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Programas
{
/**
 * @var integer $id_programa
 *
 * @ORM\Column(name="id_programa", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id_programa;

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

/**
 * @var string $id_mencion_programa
 *
 * @ORM\Column(name="id_mencion_programa", type="string")
 * 
 * @ORM\ManyToOne(targetEntity="MencionPrograma", inversedBy="Programas")
 * 
 */
private $id_mencion_programa;

/**
 * @var \DateTime $duracion_desde
 *
 * @ORM\Column(name="duracion_desde", type="time")
 */
private $duracion_desde;

/**
 * @var \DateTime $duracion_hasta
 *
 * @ORM\Column(name="duracion_hasta", type="time")
 */
private $duracion_hasta;


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

/**
 * Set nombre
 *
 * @param string $nombre
 * @return Programas
 */
public function setNombre($nombre)
{
    $this->nombre = $nombre;

    return $this;
}

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

/**
 * Set id_mencion_programa
 *
 * @param MencionPrograma $idMencionPrograma
 * 
 * @return Programas
 */
public function setIdMencionPrograma(MencionPrograma $idMencionPrograma)
{
    $this->id_mencion_programa = $idMencionPrograma;

    return $this;
}

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

/**
 * Set duracion_desde
 *
 * @param \DateTime $duracionDesde
 * @return Programas
 */
public function setDuracionDesde($duracionDesde)
{
    $this->duracion_desde = $duracionDesde;

    return $this;
}

/**
 * Get duracion_desde
 *
 * @return \DateTime 
 */
public function getDuracionDesde()
{
    return $this->duracion_desde;
}

/**
 * Set duracion_hasta
 *
 * @param \DateTime $duracionHasta
 * @return Programas
 */
public function setDuracionHasta($duracionHasta)
{
    $this->duracion_hasta = $duracionHasta;

    return $this;
}

/**
 * Get duracion_hasta
 *
 * @return \DateTime 
 */
public function getDuracionHasta()
{
    return $this->duracion_hasta;
}
}

Mi实体提到:

<?php

namespace MediterraneoFM\MediterraneoFMBundle\Entity;

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

/**
 * MediterraneoFM\MediterraneoFMBundle\Entity\MencionPrograma
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class MencionPrograma
{
/**
 * @var integer $id_mencion_programa
 *
 * @ORM\Column(name="id_mencion_programa", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 * @ORM\OneToMany(targetEntity="Programas", mappedBy="MencionPrograma")
 * 
 */
private $id_mencion_programa;

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

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

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

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

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

/**
 * Set id_emision_programa
 *
 * @param integer $idEmisionPrograma
 * @return MencionPrograma
 */
public function setIdEmisionPrograma($idEmisionPrograma)
{
    $this->id_emision_programa = $idEmisionPrograma;

    return $this;
}

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

/**
 * Set nro_menciones
 *
 * @param string $nroMenciones
 * @return MencionPrograma
 */
public function setNroMenciones($nroMenciones)
{
    $this->nro_menciones = $nroMenciones;

    return $this;
}

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

/**
 * Set nombre
 *
 * @param string $nombre
 * @return MencionPrograma
 */
public function setNombre($nombre)
{
    $this->nombre = $nombre;

    return $this;
}

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

我的控制器:

<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

namespace MediterraneoFM\MediterraneoFMBundle\Controller;

# Funciones del kernel de Symfony
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Doctrine\Common\Collections\ArrayCollection;
# Paginador
use MakerLabs\PagerBundle\Pager;
use MakerLabs\PagerBundle\Adapter\ArrayAdapter;
use MakerLabs\PagerBundle\Adapter\DoctrineOrmAdapter;
# Entities
use MediterraneoFM\MediterraneoFMBundle\Entity\Programas;
# Types
use MediterraneoFM\MediterraneoFMBundle\Form\InsertProgramasType;

class ProgramasController extends Controller {

# Alta de Clientes, insert en tabla Clientes.
public function insertAction() {
    $programas = new Programas();
    $formType = new InsertProgramasType();
    $form = $this->createForm($formType, $programas);

    return $this->render('MediterraneoFMBundle::insertProgramas.html.twig', array('form' => $form->createView()));
}

public function insertSuccessAction(Request $request) {
    # Objeto que modifico
    $programas = new Programas();
    # Formulario que modifico
    $formType = new InsertProgramasType();
    $form = $this->createForm($formType, $programas);

    if ($request->getMethod() == 'POST'):
        $form->bind($request);
        $data = $form->getData();
        if ($form->isValid()):
            $em = $this->getDoctrine()->getManager();
            $em->persist($programas);
            $em->flush();

            return $this->redirect($this->generateUrl('insertProgramas'));
        endif;
    endif;
}

/**
 *
 * @Route("/programas/show/{limit}/{page}", defaults={"page"=1}, name="showProgramas")
 * 
 */
public function showAction($page, $limit) {
    $programas = $this->getDoctrine()
            ->getRepository('MediterraneoFMBundle:Programas')
            ->findAll();

    if (!$programas):
        throw $this->createNotFoundException('No emisoras found for id ');
    endif;

    $adapter = new ArrayAdapter($programas);

    $pager = new Pager($adapter, array('page' => $page, 'limit' => $limit));

    return $this->render('MediterraneoFMBundle::showProgramas.html.twig', array('emisoras' => $programas, 'pager' => $pager));
}
}
?>

我的表格类型:

<?php

namespace MediterraneoFM\MediterraneoFMBundle\Form;

use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class InsertProgramasType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('nombre')
        ->add('id_mencion_programa', 'entity', array(
            'class' => 'MediterraneoFM\MediterraneoFMBundle\Entity\MencionPrograma',
                'property' => 'nombre',
                'query_builder' => function(EntityRepository $er) {
                    return $er->createQueryBuilder('c');
                },
                'label' => 'Mencion',
                'multiple' => true,
                'expanded' => true,
                'required' => false,
            ))
        ->add('duracion_desde', 'time', array(
            'with_seconds' => true
        ))
        ->add('duracion_hasta', 'time', array(
            'with_seconds' => true
        ))
    ;
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'MediterraneoFM\MediterraneoFMBundle\Entity\Programas',
        'csrf_protection' => false,
        'csrf_field_name' => '_token',
        // a unique key to help generate the secret token
        'intention'       => 'task_item',
    ));
}

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

因此,当我将数据保存到de数据库时,它会保存:

O:58:"MediterraneoFM\MediterraneoFMBundle\Entity\MencionPrograma":4:         {s:79:"�MediterraneoFM\MediterraneoFMBundle\Entity    \MencionPrograma�id_mencion_programa";i:2;s:66:"�MediterraneoFM\MediterraneoFMBundle\Entity\MencionPrograma�nombre";s:3:"8-1";s:79:"�MediterraneoFM\MediterraneoFMBundle\Entity\MencionPrograma�id_emision_programa";i:2;s:73:"�MediterraneoFM\MediterraneoFMBundle\Entity\MencionPrograma�nro_menciones";s:1:"1";}

或者像这样:

Doctrine\Common\Collections\ArrayCollection@0000000037e77446000000008ebafd04

我做错了什么?抱歉我的英文!

0 个答案:

没有答案
相关问题