0

我在 Symfony 中有一个表单,其中有一个使用 dropzone.js 上传图像的 dropzone,当上传图像时,图像路径以及表单所属实体的当前实例的 id 被上传到Image表中imageNamelisting_id分别。这listing_idid用户正在填写的当前列表。由于每个列表我有多个图像,我想知道是否可以保持对象和图像OneToMany之间的关系。listing

当用户单击“添加列表”时,将listing使用空值创建对象作为草稿(除了 id),然后将图像放入其中,然后将它们添加到数据库中。

清单.php

/**
 * @ORM\OneToMany(targetEntity="Image", mappedBy="listing", cascade={"persist"}, orphanRemoval=true)
 */
private $images;

图片.php

/**
 * @ORM\ManyToOne(targetEntity="Listing", inversedBy="images")
 * @ORM\JoinColumn(name="listing_id", referencedColumnName="id", onDelete="CASCADE")
 */
private $listing;

将图像放入时会导致错误,但如果我更改$listing

 /**
 * @ORM\Column(type="integer", nullable=true, name="listing_id")
 */
protected $listing;

它有效,但我的意图是稍后使用该listing对象检索图像,因此这不是预期的结果。我的问题是,即使图像对象是在与列表不同的时间(稍后)创建的,我是否仍然可以在这两个值之间保持这种耦合。实际上,当我使用这些注释将某些内容放入放置区时出现错误。

由于图像行是实时插入到表中的,而不是在表单提交时,使用这种耦合时可能会导致错误,但我不确定。

4

1 回答 1

1

你走在正确的道路上。我正在使用 oneup/uploader-bundle 来完成这项任务。

需要正确的@ManyToOne。@Column 不起作用。

但在我的用例中,它是带有附加数据的内联图像。

我不得不停用 dropzone 自动加载并手动手工制作 dropzone 调用和设置以包含其他表单字段。

还有一个选项可以在删除文件后立即开始手动上传。通过这种方式,可以通过一个帖子请求创建整个事物,而无需事先保存列表以具有 ID...

我希望它有所帮助。我可以分享我的代码,但现在不能在机器上。

编辑:这是我的代码:

var myDropzone = new Dropzone("#documentTypeUploadDropzone", {

            url : actionUrl,
            dictDefaultMessage: 'Bitte das neue Dokument hier ablegen!',

            // The configuration we've talked about above
            autoProcessQueue: false,

            maxFiles: 1,
            init: function () {
                this.on('success', function(e, serverResponse) {

                    myDropzone.removeAllFiles();

                });

                this.on('sending', function(file, xhr, formData) {
                    // Add additional data form original <form> which was already checked by html5 client side verification
                    formData.append('productId', 1);
                    formData.append('documentType[name]', value1;
                    formData.append('documentType[foreignUrl]',   $(#formInput).val());

                });

                var myDropzone = this;

                $('#documentTypeUploadForm').on('submit', function(e) {
                    // Make sure that the form isn't actually being sent.
                    e.preventDefault();
                    e.stopPropagation();


                    var files = myDropzone.getQueuedFiles();
                    if (!files.length) {
                        $.growl.error({ title:"Fehler", message: 'Bitte zuerst ein Dokument zum Hochladen auswählen.'});
                        return
                    }


                    myDropzone.processQueue();
                });


            }
        });
于 2018-09-21T02:05:39.423 回答