Git Product home page Git Product logo

Comments (11)

dustin10 avatar dustin10 commented on May 30, 2024

You could use the adapter to resolve whether or not it is a proxy and then take appropriate action.

from vichuploaderbundle.

khepin avatar khepin commented on May 30, 2024

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.

dustin10 avatar dustin10 commented on May 30, 2024

You could add an isProxy method to the Adapter that checks. This would be trivial.

from vichuploaderbundle.

makasim avatar makasim commented on May 30, 2024

+1

from vichuploaderbundle.

oopsFrogs avatar oopsFrogs commented on May 30, 2024

HI, I encountered the same problem, the twig helper could not get the filename property.

from vichuploaderbundle.

ornj avatar ornj commented on May 30, 2024

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.

ornj avatar ornj commented on May 30, 2024

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.

stevecorenflos avatar stevecorenflos commented on May 30, 2024

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.

dustin10 avatar dustin10 commented on May 30, 2024

https://github.com/dustin10/VichUploaderBundle/blob/master/Adapter/ORM/DoctrineORMAdapter.php#L42

from vichuploaderbundle.

stevecorenflos avatar stevecorenflos commented on May 30, 2024

Thank you!

from vichuploaderbundle.

egeloen avatar egeloen commented on May 30, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.