关系 'OneToMany' Symfony2 - 错误

Relationship ' OneToMany ' Symfony2 - Error

我的 symfony2 项目中有这个错误:

The form's view data is expected to be an instance of class Proyecto\AdminBundle\Entity\Unidades, but is an instance of class Proyecto\AdminBundle\Entity\Catalogo. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms an instance of class Proyecto\AdminBundle\Entity\Catalogo to an instance of Proyecto\AdminBundle\Entity\Unidades.

消除了一些东西,如实体的 getter、setter 和属性。

Catalogo.php

    class Catalogo
    {
     /**
     * @ORM\OneToMany(targetEntity="Unidades", mappedBy="catalogo")
     */
    protected $unidades;

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

    public function getUnidades()
    {
        return $this->unidades;
    }

    public function setUnidades($unidades)
    {
        $this->unidades[] = $unidades;
        foreach ($unidades as $unidad) {
        $unidad->setCatalogo($this);
        }
    }

Unidades.php

class Unidades
{
    /**
    * @ORM\ManyToOne(targetEntity="Catalogo", inversedBy="unidades")
    * @ORM\JoinColumn(name="catalogo_id", referencedColumnName="id")
    */
    protected $catalogo;

   /**
     * Set catalogo
     *
     * @param float $catalogo
     * @return Unidades
     */
    public function setCatalogo($catalogo)
    {
        $this->catalogo = $catalogo;

        return $this;
    }

    /**
     * Get catalogo
     *
     * @return float 
     */
    public function getCatalogo()
    {
        return $this->catalogo;
    }

CatalogoUnidadesType.php

<?php

namespace Proyecto\AdminBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;


class CatalogoUnidadesType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) 
    {
        $builder->add('catalogo', new CatalogoType());
        $builder->add('unidades', new UnidadesType());

    }

    public function setDefaultOptions(\Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Proyecto\AdminBundle\Entity\Unidades'
        ));
    }

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

}

CatalogoType.php

<?php

namespace Proyecto\AdminBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;


class CatalogoType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) 
    {
        $builder->add('articulo');
        $builder->add('descripcion');

    }

    public function setDefaultOptions(\Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Proyecto\AdminBundle\Entity\Catalogo'
        ));
    }

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

}

UnidadesType.php

<?php

namespace Proyecto\AdminBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;


class UnidadesType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) 
    {
        $builder->add('medida');
        $builder->add('precio');

    }

    public function setDefaultOptions(\Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Proyecto\AdminBundle\Entity\Unidades'
        ));
    }

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

}

AdminController.php

public function agregarProductoCategoriaAction($tipo)
    {
        $request = $this->getRequest();
        $catalogo = new Catalogo();
        $unidades = new Unidades();
        $catalogo->setUnidades($unidades);
        $form = $this->createForm(new CatalogoUnidadesType(), $catalogo);

        /**........

        **/

       return $this->render('ProyectoAdminBundle:Admin:agregarProductoCategoria.html.twig', array('form' => $form->createView(), 'tipo' => $tipo));
    }

您应该构建一个名称为 CatalogoUnidades 的 class,其中包括 CatalogUnidades,然后为其创建一个 FormType。

希望对您有所帮助。