I
I
Igor2019-07-09 01:55:23
Doctrine ORM
Igor, 2019-07-09 01:55:23

How to solve the problem ArrayCollection::__construct() must be of the type array, object given?

The problem is the following ...
Everything is as in the manual, maybe there are some inconsistencies, I would like to find them
5d23c8960da52018729312.png
. There is nothing to procrastinate
here, here is the code

...
    /**
     * @ORM\ManyToMany(targetEntity="App\Model\Extras\Extras")
     * @ORM\JoinTable(
     *     name="product_extras", joinColumns={
     *          @ORM\JoinColumn(name="product_id", referencedColumnName="id")},
     *     inverseJoinColumns={
     *          @ORM\JoinColumn(name="extras_id", referencedColumnName="id")}
     * )
     */
    private $extras;

    /**
     * Product constructor.
     */
    public function __construct()
    {
        $this->extras = new ArrayCollection();
    }

    /**
     * @param Extras $extras
     */
    public function addExtras(Extras $extras = null): void
    {
        $this->extras->add($extras);
    }

Further...
/** @var Extras $item */
        foreach ($this->extrasRepository->findBy(["id" => $extras_criteria]) as $item){
            $product->addExtras($item);
        }

Well, such a banal thing that it's even embarrassing.
Doctrine requires me that $product->extras be an array (ArrayCollection)
What's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor, 2019-07-09
@IgorPI

The answer, be careful, maybe it's not the suspected code section, as in my case.
I assumed that the error crept in foreach ...
Since the error mentioned "ArrayCollection::__construct()"
The error was in $product->
setProductType There is no ArrayCollection in the $product->setProductType method

$product = new Product();
        $product->setProductType($this->productTypeRepository->getById($this->request->mixed("productType")));
        $product->setName($this->request->mixed("name"));
        $product->setPrice($this->request->mixed("price"));
        $product->setCountMin($this->request->mixed("countMin"));
        $product->setCountMax($this->request->mixed("countMax"));
        $product->setShortDescription($this->request->mixed("shortDescription"));
        $product->setLongDescription($this->request->mixed("longDescription"));

        $extras_criteria = explode(",", $this->request->mixed("extras")) ;

        /** @var Extras $item */
        foreach ($this->extrasRepository->findBy(["id" => $extras_criteria]) as $item){
            $product->getExtras()->add($item);
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question