当multiple为true时,无法使用实体形式插入多个值

时间:2016-02-25 14:12:44

标签: forms symfony doctrine-orm

当我使用multiple = true提交表单时会抛出一个错误:当为false时,一切都按预期工作,并将一个值插入表中。

  

捕获致命错误:传递给AppBundle \ Entity \ CustomersProducts :: setProducts()的参数1必须是AppBundle \ Entity \ Products的实例,Doctrine \ Common \ Collections \ ArrayCollection的实例,在/ var / www /中调用第556行的html / app / vendor / symfony / symfony / src / Symfony / Component / PropertyAccess / PropertyAccessor.php已定义

Products.php

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use AppBundle\Entity\Products;

/**
 * Products
 *
 * @ORM\Table(name="products")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ProductsRepository")
 */
class Products
{

     /**
     * @ORM\OneToMany(targetEntity="CustomersProducts", mappedBy="products")
     */
    private $products;
    
	
	public function __toString() {
    return $this->name;
}


	
	public function addProducts(Products $request)
{

    $this->products->add($request);
    return $this;
}

public function removeProducts(Products $request)
{
    $this->products->removeElement($request);
    return $this;
}

    public function __construct() {
        $this->products= new 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;
use AppBundle\Entity\Customers;
use AppBundle\Entity\Products;

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

    /**
     * @ORM\ManyToOne(targetEntity="Customers", inversedBy="customers")
     * @ORM\JoinColumn(name="customer_id", referencedColumnName="id")
     */
    private $customers;
	
public function getCustomers()
{
    return $this->customers;
}

public function setCustomers(Customers $customer) // Please add a Use statement on top of your document
{
    $this->customers = $customer;
    return $this;
}


	
	/**
     * @ORM\ManyToOne(targetEntity="Products", inversedBy="products")
     * @ORM\JoinColumn(name="product_id", referencedColumnName="id")
     */
    private $products;
 
 
 public function getProducts()
{
    return $this->products;
}

public function setProducts(Products $products) // Please add a Use statement on top of your document
{

var_dump($products->getName()); die();
    $this->products = $products;
    return $this;
}

    /**
     * @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;
    }
}

CustomersProductsType.php

<?php

namespace AppBundle\Form;

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

class CustomersProductsType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('version')
              ->add('customers', 'entity', array(
			                         'class' => 'AppBundle:Customers',
									 'multiple' =>false,
									 'property' => 'name',
									
			
			))
			
            ->add('products', 'entity', array(
			                         'class' => 'AppBundle:Products',
									 'multiple' =>true,
									 'property' => 'name',
									
			
			))
			
        ;
    }
    
    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\CustomersProducts'
        ));
    }
}

更新:

改变之后我现在从建议中得到以下错误:

  

在关联AppBundle \ Entity \ CustomersProducts#products上找到Doctrine \ Common \ Collections \ ArrayCollection类型的实体,但期待AppBundle \ Entity \ Product

1 个答案:

答案 0 :(得分:0)

您必须将实体产品重命名为Product,因为表示一个数据库行(实体)的类应具有单数名称。

语法错误的方法名称addProducts:

public function addProducts(Products $request)

将其重构为:

 public function addProduct(Product $product) {

  if ($this->products->contains($product)) {
            return;
  }
  $this->products->add($product);
}

与删除方法相同:

public function removeProducts(Products $request)

重构:

public function removeProduct(Product $product) {
  if (!$this->products->contains($product)) {
              return;
  }
  $this->products->removeElement($product);
}

对CustomersProducts的更改

/**
 * @ORM\ManyToOne(targetEntity="Product", inversedBy="products")
 * @ORM\JoinColumn(name="product_id", referencedColumnName="id")
 */
 private $product;


 public function getProduct()
 {
    return $this->product;
 }

 public function setProduct(Product $product)
 {
     $this->product = $product;
 }

您可以在article

中找到类似的示例和说明