Symfony 3.3: FileNotFoundException after moving/renaming uploaded file - symfony

I have a form with a file upload field (limited to PDF format only). After the form has been submitted and a valid uploaded file is present, I rename and move the file. Then I try to display the page with the form again - at which point the Symfony\Component\HttpFoundation\File\File class constructor throws a FileNotFoundException because, for some reason, it's been handed the path to the no-longer-existing temp file.
Relevant facts:
The form fields (including the file upload field) are not mapped to an entity because I'm using a Wordpress-style "meta" table for the additional data associated with the entity; the controller handles creating any new objects to be persisted.
The new filename is successfully saved to the database; I've further verified that the error occurs with the $form->createView() call.
Before anyone suggests that this was due to my PHP settings, note that I purposefully tried uploading a PDF file with a size below the limit.
My temporary solution is to redirect to another page (there is no error when I do this), but longterm it's much more ideal to still show the form page after submitting the form.
I tried overwriting the UploadedFile object I got from the form with the File object returned by the ->move() method, but this didn't help.
I also tried creating a child class of the built-in FileType class and changing the data_class to be SplFileInfo (the parent class of Symfony's File class) b/c SplFileInfo doesn't throw an exception over an invalid path, but this had no effect either.
My interpretetation of what's happening is that, for some reason, when you create a form view Symfony instantiates a new File object for the file upload field using the old, temporary file path - thus resulting in a fatal error that absolutely shouldn't be happening (because when would you ever not be moving the uploaded file?).
Any suggestions for things to try that I haven't thought of yet would be much appreciated!
Solved
I figured out the problem by looking at the stack trace - I have a Twig extension that was calling Request::createFromGlobals(), and THAT resulted in trying to create a new UploadedFile object with the no-longer-existing temp file path. Having the extension get the already-existing Request object in its constructor should prevent this.

Related

Executing a method which is named via a config file

In short: I have a method name provided via a JSON configuration file. I'd like to call a method using this provided name. The method (with a matching name) will exist in the backend. What's the best way of going about this?
I am not quite sure what I should be searching for as an example.
To detail: I am working with a legacy application, hence the VB.NET. I am building a single PDF file from multiple PDF sources. Most of these are as is, I simply read the configuration and grab the relevant files and the job is done. However some require processing, I'd like the configuration file to pass in a method name to be called that will perform extra processing on the PDF, whatever that may be.
As there can be a lot of PDF files that can vary, I cannot simply use a property such as "PostProcessing: true".
Any ideas?
You could use reflection to reflect method names back and check them against the name passed from the property in the config file.
Like so
Type magicType = Type.GetType("MagicClass");
MethodInfo magicMethod = magicType.GetMethod("ItsMagic");
object magicValue = magicMethod.Invoke(magicClassObject, new object[]{100});
That would work.. but to be honest, I'd go with a case statement as you'll be hardcoding the method names anyway (because they are code), and it'll be strongly typed (less chance of typos and errors).

EntityMetadataWrapperException: Invalid data value given

On editing a node with a user of a particular role i get the following error a Drupal site. With only user 1 it works. Can't figure why. I debug the entity file, but it seems that the id disappear after iteration.
EntityMetadataWrapperException : Invalid data value given. Be sure it matches the required data type and format. dans EntityDrupalWrapper->set() (ligne 737 dans /sites/all/modules/entity/includes/entity.wrapper.inc).
No coding yet has been done for this only using contrib modules.
Since you haven't done any coding, it's probably bug of some contrib module, Entity API or you're dealing with malformed entity.
Try applying this patch: Add field information to exception message on validation exception, this will allow you to see on which value it fails.
In case you're using some custom coding, then check Entity metadata wrappers page for some examples. The common mistake is to not use array() for set() when dealing with multi-valued field, or opposite - extra array for single-valued field.
If you can reproduce the problem, you can debug the issue either by step-by-step debugger and do the breakpoint on Exception line, or do it manually by temporary adding: var_dump(debug_backtrace()); exit; just before the exception happens, so PHP can dump the backtrace of your current code with all the argument passed, so you can track your malformed entity or identify the failing contrib module.
See also: How to set a value on a field collection using entity metadata wrapper at DA

Plone: processForm() deletes my Archetype field

I've written a product that uses an ATFolderSchema. The schema contains a costum archetypes field.
I implemented an edit form using content_edit. It works fine, but if i call content_edit, the content of my costum archetypes field is deleted. I could figure out that the function call new_context.processForm() in Archetypes/skins/archetypes/content_edit_impl.py causes this problem.
Unfortunately I can't find any information about processForm() in the internet.
I use Plone 4.1.6 and Archetypes 1.7.14.
Could you help me?
The processForm method is defined on Archetypes BaseObject
It basically handles the event triggering + creationFlag.
Th code you mentioned is in _processForm called by processForm
You can place a debugger for example on line 600
your field has to be in fields and the data in form.
My best guess so far is, since you have your own content_edit, that you have a naming issue.
You can test this by temporary disable (remove) your custom content_edit and check if your data is stored on the object.

Symfony2/Doctrine: How to persist an entity from the entity class?

Short Description of the problem:
I generate a file inside the entity class and would like to save the filename to the database. The controller doesn't know about this (wheter or not the filename has changed, so it's not practical to persist from the controller.
Is there a way for an Entity to persist itself?
Example of my use:
The entity class is for an image in a gallery. I always keep the original file and work with a cached version of the file. When the image is changed (rotated for example), the cached version is deleted. The cached version also be deleted in other cases. When the file is needed, I check if the cached file exists, otherwise it is regenerated with a new filename from the archived image. I need a new filename because that resets the cache for various thumbnail sizes.
When I generate that new file, I have to save its filename to the database somehow. Because it is only decided in the Entity when to regenerate the image, it would be practical if the entity could persist itself to the database, but I haven't found a solution for that.
Is there a way to do this or is there a whole different concept I should be using to regenerate the image file?
Entities in Doctrine are not active records - they cannot perform persistance actions by themselves, so they rely on a Big Brother [the entity manager].
Even if the controller doesn't know wether any filename as changed or not, you do - just persist your picture every time: if nothing changed, Doctrine won't touch the entity.
Have a look at lifecycle events too, maybe you can find useful to fire a #PreUpdate method before persistance [e.g. generating thumbnails].

How do I call GetLocalResource in another class

So I have Test1.aspx, Test1.aspx.vb. The LocalResource files, in the App_LocalResources folder, Test1.aspx.resx and Test1.aspx.es.resx. I also have a class called TestTheData.vb in the App_Code folder.
Now what I want to do is call GetLocalResource("stringObjRes").ToString in the TestTheData.vb class. The method however is not showing up in Intellisense. When I try to type manually, I get the error lines in my code.
I've imported:
Globalization
Threading
Threading.Thread
Web
Web.UI.Page.
No luck. So how I am supposed to do this....?
Well it seems that Local Resources can't be accessed in files that are in the App_Code folder. So I used Global Resources instead.
I know it's 1 year old but I just added the comment if some others are also searching for this:
Your guess is right, you cannot access the Local Resource Object from another class. GetLocalResourceObject only exists within the code of the page, in your case Test1.aspx.vb. If you are calling the class function from your Test1.aspx.vb you could of course retrieve the Local resource from there and then supply it to your TestTheData.vb as a parameter. But if you need the 'stringObjRes' in several places (not only in Test1.aspx) then a global resource is of course preferred. Details here: http://msdn.microsoft.com/en-us/library/ms227982(v=vs.100).aspx

Resources