Doctrine / Symfony2:尝试持久化多个元素时出现异常

时间:2016-01-31 19:47:13

标签: php symfony doctrine-orm doctrine

我尝试将多张照片上传到产品但不起作用 这个例外是:

例外:

  

在关联... \ Entity \ Product#photos上找到Doctrine \ Common \ Collections \ ArrayCollection类型的实体,但期待.... \ Entity \ Photos   500内部服务器错误 - ORMException

我有2个实体:

产品:

 /**
 * @ORM\OneToMany(targetEntity="Photos", mappedBy="product")
 */
private $photos;

照片

/**
 * @ORM\ManyToOne(targetEntity="Product", inversedBy="photos")
 * @ORM\JoinColumn(nullable=true)
 */
private $product;

,在我的控制器中:

......

$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();

堆栈跟踪(纯文本):

  

[1] Doctrine \ ORM \ ORMException:找到关于.. \ Entity \ Product#photos的Doctrine \ Common \ Collections \ ArrayCollection类型的实体,但期待Portfolio \ Backend \ BackendBundle \ Entity \ Photos       在n / a           在C:.. \ vendor \ doctrine \ orm \ lib \ Doctrine \ ORM \ UnitOfWork.php第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
  

对象(Doctrine \ Common \ Collections \ ArrayCollection)[509]私有   'elements'=>       数组(大小= 2)         'file'=>           对象(学说\共同\类别\ ArrayCollection的)[603]             私人'元素'=>               数组(大小= 1)                 ...         0 =>           对象(.. \实体\媒体)[740]             私人'id'=>空值             私人'档'=>               对象(Symfony的\元器件\ HttpFoundation \文件\ UploadedFile的)[13]                 ...             私人'产品'=>空

您对这个例外有所了解吗?

1 个答案:

答案 0 :(得分:0)

首先,您应该为@JoinColumn添加所有必需的属性:

/**
 * @ORM\ManyToOne(targetEntity="Product", inversedBy="photos")
 * @ORM\JoinColumn(name="product_id", referencedColumnName="id", nullable=true)
 */
private $product;

然后再次检查您的脚本。