Answer the question
In order to leave comments, you need to log in
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
. 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);
}
/** @var Extras $item */
foreach ($this->extrasRepository->findBy(["id" => $extras_criteria]) as $item){
$product->addExtras($item);
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question