Reputation: 73
I tried to upload multiples photos to a product but it does not work this Exception is :
Exception:
Found entity of type Doctrine\Common\Collections\ArrayCollection on association ...\Entity\Product#photos, but expecting ....\Entity\Photos 500 Internal Server Error - ORMException
I have 2 Entities :
Product :
/**
* @ORM\OneToMany(targetEntity="Photos", mappedBy="product")
*/
private $photos;
Photos :
/**
* @ORM\ManyToOne(targetEntity="Product", inversedBy="photos")
* @ORM\JoinColumn(nullable=true)
*/
private $product;
and in my Controller :
......
$files = $form->get('photos')->getData(); // I use Transformer to transform data to Photos Entity
foreach($files as $list){
foreach($list as $item){
$product->addPhoto($item);
}
}
$em->persist($product);
$eù->flush();
Stack Trace (Plain Text):
[1] Doctrine\ORM\ORMException: Found entity of type Doctrine\Common\Collections\ArrayCollection on association ..\Entity\Product#photos, but expecting Portfolio\Backend\BackendBundle\Entity\Photos at n/a in C:..\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 791
at Doctrine\ORM\UnitOfWork->computeAssociationChanges(array('fieldName' => 'photos', 'mappedBy' => 'product', 'targetEntity' => '..\Entity\Photos', 'cascade' => array(), 'orphanRemoval' => false, 'fetch' => '2', 'type' => '4', 'inversedBy' => null, 'isOwningSide' => false, 'sourceEntity' => '..\Entity\Product', 'isCascadeRemove' => false, 'isCascadePersist' => false, 'isCascadeRefresh' => false, 'isCascadeMerge' => false, 'isCascadeDetach' => false), object(PersistentCollection))
in C:\..\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 687
at Doctrine\ORM\UnitOfWork->computeChangeSet(object(ClassMetadata), object(Product))
in C:\..\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 404
at Doctrine\ORM\UnitOfWork->computeScheduleInsertsChangeSets()
in C:\..\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 711
at Doctrine\ORM\UnitOfWork->computeChangeSets()
in C:\..\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php line 297
at Doctrine\ORM\UnitOfWork->commit(null)
in C:\..\vendor\doctrine\orm\lib\Doctrine\ORM\EntityManager.php line 340
at Doctrine\ORM\EntityManager->flush()
in C:\..\Controller\ProductController.php line 516
at ..\Controller\PostsController->createAction(object(Request))
in line
at call_user_func_array(array(object(ProductController), 'createAction'), array(object(Request)))
in C:\..\app\bootstrap.php.cache line 3094
at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), '1')
in C:\..\app\bootstrap.php.cache line 3056
at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1', true)
in C:\..\app\bootstrap.php.cache line 3207
at Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle(object(Request), '1', true)
in C:\..\app\bootstrap.php.cache line 2429
at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
in C:\..\web\app_dev.php line 28
object(Doctrine\Common\Collections\ArrayCollection)[509] private 'elements' => array (size=2) 'file' => object(Doctrine\Common\Collections\ArrayCollection)[603] private 'elements' => array (size=1) ... 0 => object(..\Entity\Medias)[740] private 'id' => null private 'file' => object(Symfony\Component\HttpFoundation\File\UploadedFile)[13] ... private 'product' => null
You have an idea for this exception?
Upvotes: 2
Views: 767
Reputation: 131
First you should add all required attributes for @JoinColumn:
/**
* @ORM\ManyToOne(targetEntity="Product", inversedBy="photos")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id", nullable=true)
*/
private $product;
Then check your script again.
Upvotes: 0