如何使用symfony2表单填充一对多/多对一表?

时间:2016-02-24 14:29:48

标签: forms symfony doctrine-orm

我是Symfony的新手,我不得不填充一对多/多对一的表格。 我设法显示表单但是当我提交表单时出现以下错误:

  

在关联AppBundle \ Entity \ Customers#customers_products上找到AppBundle \ Entity \ Products类型的实体,但期待AppBundle \ Entity \ CustomersProducts

这是我的代码:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\CustomersProducts;

/**
 * Customers
 *
 * @ORM\Table(name="customers")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\CustomersRepository")
 */
class Customers
{


 /** @ORM\OneToMany(targetEntity="CustomersProducts", mappedBy="customers") */

 protected $customers_products;

  public function __construct()
    {
        $this->customers_products = new \Doctrine\Common\Collections\ArrayCollection();
    } 


   public function getCustomersProducts()
    {
        return $this->customers_products->toArray();
    }

 public function setCustomersProducts($customers_products)
    {
     if (count($customers_products) > 0) {
        foreach ($customers_products as $i) {
            $this->addCustomersProducts($i);
        }
    }

    return $this;
    } 


    public function addCustomersProducts( $customers_products)
{


    $this->customers_products->add($customers_products);
} 

    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

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


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

    /**
     * Set name
     *
     * @param string $name
     * @return Customers
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

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

    /**
     * Set ip
     *
     * @param string $ip
     * @return Customers
     */
    public function setIp($ip)
    {
        $this->ip = $ip;

        return $this;
    }

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

Products.php:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
 * Products
 *
 * @ORM\Table(name="products")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ProductsRepository")
 */
class Products
{


  /** @ORM\OneToMany(targetEntity="CustomersProducts", mappedBy="products") */
    protected $customers_products; 

     public function __construct()
    {
        $this->customers_products = new \Doctrine\Common\Collections\ArrayCollection();
    }
    /** 
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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


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

    /**
     * Set name
     *
     * @param string $name
     * @return Products
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

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

CustomersProducts.php

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * CustomersProducts
 *
 * @ORM\Table(name="customers_products")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\CustomersProductsRepository")
 */
class CustomersProducts
{



    /** 
     * 
     * @ORM\ManyToOne(targetEntity="Customers", inversedBy="customers_products") 
     * @ORM\JoinColumn(name="customer_id", referencedColumnName="id", nullable=false) 
     */
    protected $customers;

    /** 
     * 
     * @ORM\ManyToOne(targetEntity="Products", inversedBy="customers_products") 
     * @ORM\JoinColumn(name="product_id", referencedColumnName="id", nullable=false) 
     */
    protected $products;

    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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


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

    /**
     * Set version
     *
     * @param string $version
     * @return CustomersProducts
     */
    public function setVersion($version)
    {
        $this->version = $version;

        return $this;
    }

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

CustomersType.php

<?php

namespace AppBundle\Form;

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

class CustomersType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('ip')
            ->add('customers_products', 'entity', array(
                         'class' => 'AppBundle:Products',
                                     'multiple' =>true,
                                     'expanded' => true,
                                     'property' => 'name'           
            ));
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Customers'
        ));
    }
}

0 个答案:

没有答案