Comments (11)
You could use the adapter to resolve whether or not it is a proxy and then take appropriate action.
from vichuploaderbundle.
I just took a look at the adapter class but not sure I understand how and where it would play its role.
It seems the adapter is called to set the correct properties on the object inside of the listener, but in this case, the object has not been loaded yet, and therefore postLoad is not happening. Or am I missing something here?
from vichuploaderbundle.
You could add an isProxy method to the Adapter that checks. This would be trivial.
from vichuploaderbundle.
+1
from vichuploaderbundle.
HI, I encountered the same problem, the twig helper could not get the filename property.
from vichuploaderbundle.
I've had limited success with setting the fetch mode. If you do it in your entity then it will apply every time the entity is loaded, which might not be ideal.
/**
* @ORM\OneToMany(targetEntity="Upload", mappedBy="upload", fetch="EAGER")
*/
You can also temporarily set the fetch mode in your entity repository although I'm finding that this doesn't seem to apply 100% of the time.
use Doctrine\ORM\Mapping\ClassMetadata;
// ...
return $qb->getQuery()
->setFetchMode('Acme\WhateverBundle\Entity\Whatever', 'upload', ClassMetadata::FETCH_EAGER)
->getSingleResult();
The Doctrine documentation says that this is only for ManyToOne or OneToOne associations, for others you can try doing a LEFT JOIN on the association.
from vichuploaderbundle.
Here's an example repository function because this can be a bit confusing. My set up is this, I have an entity Room with a OneToMany association to Artwork, which has a OneToOne association to Audio. Audio is my entity with a file upload. Is this the most intelligent set up? I don't know. Probably not. But this function allows me to query for 1 room and use the adapter to get the full path to the uploaded audio file later in my Twig template.
public function findOneJoined($id) {
$qb = $this->getEntityManager()->createQueryBuilder()
->select('r', 'art')
->from('RoomBundle:Room', 'r')
->leftJoin('r.artwork', 'art')
->leftJoin('r.audio', 'a')
->where('r.id = :id')
->setParameter('id', $id);
try {
return $qb->getQuery()
->setFetchMode('ArtworkBundle\Entity\Artwork', 'audio', ClassMetadata::FETCH_EAGER)
->getSingleResult();
}
catch (NoResultException $e) {
return null;
}
}
As you can see, in my case I had to use both methods to get what I wanted.
from vichuploaderbundle.
I'm still not understanding the solution (nor probably even fully understanding the problem). My implementation is basically exactly the same in the entity, controller, and twig template as in the example code except the twig call is nested under a twig for loop:
{% for entity in entities %}
<td>{{ vich_uploader_asset(entity, 'myFile') }}</td>
{% endfor %}
The entity in my DB has a property for myFileName but not myFile (which is the annotated Vich\UploadableField property), which should be proper configuration for the bundle.
My understanding from reading this and gavD's thread is that when twig is populating the "entity" in the loop, it's only populating what comes out of the database and for some reason not connecting to the 'myFile' property via Vich.
The solution @dustin10 mentions is putting an isProxy method in the Adapter, but I'm not sure what condition I should be testing for? Whether 'myFile' exists?
I apologize if this is a lot of hand-holding. I'm still very new to Symfony. This seems like it would be a fairly common use scenario, so maybe it would help others as well if you could spell it out a bit more for me.
from vichuploaderbundle.
https://github.com/dustin10/VichUploaderBundle/blob/master/Adapter/ORM/DoctrineORMAdapter.php#L42
from vichuploaderbundle.
Thank you!
from vichuploaderbundle.
I just dig into this issue this afternoon. @dustin10 your solution does not fix all issues about proxy objects. The last one is about proxy initialization. Currently, if we pass a proxy uninitialized to the twig helper, the value always ends up with null as the object is not initialized & the initialization is only triggered when using getters AFAIK. IMO, we should trigger the entity loading in the doctrine adapter (L42) if it is not initialized. What's your opinion?
from vichuploaderbundle.
Related Issues (20)
- Support for Symfony 7 HOT 6
- Entity association not deleted when removing file HOT 3
- Deprecation notice in `CacheWarmer::warmUp()` with Symfony 6.4 HOT 1
- download_uri should use publicUrl when using FlysytemStorage HOT 1
- Removing a file from within the message queue handler HOT 7
- Namer is required in configuration from 2.2 to 2.3 HOT 2
- Problem during reading with flysystem HOT 2
- Invalid Schema error while installing the bundle on Symfony 7 HOT 1
- The option "required" does not exist ? HOT 3
- Installation instructions are outdated HOT 2
- Document how to get s3 URL HOT 2
- Issue with VichUploader: unsupported MIME type "image/jpeg" HOT 7
- Install dose not register in bundle.php HOT 1
- Problem with delete when using multiple mappings on one entity HOT 1
- Using pure Annotation Does not Upload the actual file. @Vich\UploadableField HOT 3
- File injection on load not working reliabely with lazy ghost objects
- How to use metadata with VichUploader HOT 9
- The attribute reader should not trigger an exception for other attributes HOT 3
- Form type lazy load support HOT 3
- publicUrl method check looks for wrong exception.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vichuploaderbundle.