0
votes

It throws an error when I submit the form with multiple=true: When is false everything works as expected and one value is inserted to the table.

Catchable Fatal Error: Argument 1 passed to AppBundle\Entity\CustomersProducts::setProducts() must be an instance of AppBundle\Entity\Products, instance of Doctrine\Common\Collections\ArrayCollection given, called in /var/www/html/app/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php on line 556 and defined

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'
        ));
    }
}

UPDATE:

After the change I'made from the suggestion now I get the following error:

Found entity of type Doctrine\Common\Collections\ArrayCollection on association AppBundle\Entity\CustomersProducts#products, but expecting AppBundle\Entity\Product

1

1 Answers

0
votes

You have to rename entity Products to Product, because class that representing one database row (an entity) should have singular name.

Grammatically incorrect method name addProducts:

public function addProducts(Products $request)

refactor it to:

 public function addProduct(Product $product) {

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

The same with remove method:

public function removeProducts(Products $request)

refactor to:

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

Changes to 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;
 }

You can find similar example and explanation in the article