Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[cookbook] fixed file uploads with Doctrine samples to upload the file wh#564

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
weaverryan merged 2 commits intosymfony:masterfromhhamon:cookbook_upload_fix
Jul 30, 2011

Conversation

@hhamon
Copy link
Contributor

[cookbook] fixed file uploads with Doctrine samples to upload the file when the object is both persisted and updated. Added a note about PrePersist, PreUpdate, PostPersist and PostUpdate lifecycle callback events.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

probably meant "updated" :)

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Thanks fixed.

@weaverryan
Copy link
Member

Hey Hugo!

Ok, so you're definitely correct with this PR, but to be a truly functional system, should we also include something that would handle unlinking the old file when a new file is updated? I believe with this, each time you "update" the record by uploading a new file, the old file will still remain on the filesystem.

Thoughts?

Hugo Hamon added2 commitsJuly 27, 2011 08:42
…e when the object is both persisted and updated. Added a note about PrePersist, PreUpdate, PostPersist and PostUpdate lifecycle callback events.
@hhamon
Copy link
ContributorAuthor

Hi Ryan,

I'm ready with this PR. I have fixed it and tested it while I was giving a training. We were trying the file uploads with Doctrine and we noticed the update was not proceeded because of the missing @PreUpdate and@postupdate lifecycle callbacks.

You can merge the PR and then I will open a new PR to remove the old file on the filesystem in case of the entity is updated with a new file upload with a different name.

Cheers.

@hhamon
Copy link
ContributorAuthor

In fact, don't merge it now... I need to recheck the code.

@hhamon
Copy link
ContributorAuthor

So I rechecked and the code is good but there is a "bug" in Doctrine2. The pre and post update callbacks are only called if at least one property of the object has changed. If we try to only change the uploaded file without changing the other properties, the callbacks are not called... and that sucks...

I will add a note in the PR.

@stof
Copy link
Member

it is not a bug but a feature. The update event is dispatched only if the entity is updated, not for all entities that were queried during the request.

@hhamon
Copy link
ContributorAuthor

Yes it's a feature and a quite logic behavior. Nevertheless, if we want to trigger the callbacks we have to use a hack. For example, we can rely on an updatedAt field in the entity that is changed everytime the file is uploaded.

public function setFile(UploadedFile $file){    $this->file = $file;    $this->updatedAt = new \DateTime();}

But it's still a hack and it's probably not the best solution to consider the lifecycle callbacks when dealing with file uploads and doctrine entities...

weaverryan added a commit that referenced this pull requestJul 30, 2011
[cookbook] fixed file uploads with Doctrine samples to upload the file wh
@weaverryanweaverryan merged commit4f89114 intosymfony:masterJul 30, 2011
@weaverryan
Copy link
Member

Hi Hugo!

Thanks for the good research, I understand the problem. I've merged in this PR - the entry is obviously not perfect yet, but it's better. I've added a caution message temporarily:808da21

Perhaps this is a conversation for the dev-board. What we need is an agreed-upon "best practice" for this situation. So far, the example in this entry really doesn't fully solve the problem.

Thanks!

@weaverryan
Copy link
Member

Also see#600, so we can keep this on our radar.

@hhamon
Copy link
ContributorAuthor

Great! Thanks Ryan ;)

@lancergr
Copy link

I think that another solution that may be included in the documentation is to update the already existing path field instead of using "updatedAt":

public function setFile(UploadedFile $file) {    if ($oldFile = $this->getAbsolutePath()) {        unlink($oldFile);    }    $this->file = $file;    $this->setPath("");}

@murilolobato
Copy link
Contributor

I'm having a problem with the following situation:

On the edit screen of a entity that has OneToOne association to documents, if I click save without adding an image a register is created at the database on document, but path stays null, because i did not selected the image. What I want is dont create any entry on the 'document' table. Is there a way to do this?

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

5 participants

@hhamon@weaverryan@stof@lancergr@murilolobato

[8]ページ先頭

©2009-2025 Movatter.jp