2

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?

4
  • can you post the full text of the exception that you're getting? Commented Jan 31, 2016 at 20:46
  • @xocasdashdash I edited my question Commented Jan 31, 2016 at 21:06
  • After checking your edited answer you should double check your mapping, it can get somewhat complicated and you might need to flush twice (although you shouldn't) This: 'isCascadePersist' => false is why I suspect this to be the problem Commented Jan 31, 2016 at 21:09
  • @xocasdashdash I do not understand why in my property photos i have 2 attributes file type ArrayCollection and Photos Object Commented Jan 31, 2016 at 21:25

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.