1

I trying to load an Object with manyToOne, but become null.

#ConversationMessage.orm.yml:
AppBundle\Entity\ConversationMessage:
    type: entity
    manyToOne:
        user:
            targetEntity: User
            joinColumn:
                name: user
                referencedColumnName: id
        conversation:
            targetEntity: Conversation
            joinColumn:
                name: conversation
                referencedColumnName: id
    table: null
    repositoryClass: AppBundle\Repository\ConversationMessageRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        message:
            type: text
        time:
            type: integer
    lifecycleCallbacks: {  }

The conversation is always set to database entry. But User object not. Are both classes the same?

The User object only appears in the ConversionMessage if it is the User self. All objects, the userID isn't my logged in userID, the entry isn't initialized.

I have tried to set fetch method to EAGER, but the same result.

Can someone Help?

Thank!

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Stephan B
  • 21
  • 3

2 Answers2

2

Doctrine implements a technique called LazyLoading. It loads data in the moment you need it, not before to avoid loading unnecessary thing or loading them at a suboptimal time.

I think a good analogy for this is when a browser loads images on a site only when they become visible in the viewport for the first time. Before lazy loading, the browser would have loaded all images at page load and increased load times doing that, even thought the user might actually never need them because they are never rendered, because they never scrolled far enough down for example.

Doctrine works with accessors (getUser(), setUser()). The actual data gets loaded only when the getter it called the first time. Before that, only a reference is stored in conversations.user. That is why all the other fields are null until their getter is called.

Since dump() just dumps the whole object in the first parameter, it does not care about how the object is intended to be used.

TL:DR; Its a performance optimization measure. Always use your getters, never access your entities class variables directly (in fact, they should be private).

Some more in this: https://stackoverflow.com/a/46175874/5641300

DumperJumper
  • 75
  • 11
0

Many try's later... When I use:

{{ dump(conversations) }}

The user-Object shows all fields as "null". Only ID is set to the right value. When the user Object is my user Object, all fields set with my data.

But I can access

{{ conversations.getUser().getUsername() }}

And the username will be shown. An

{{ dump(conversations) }}

After reading Username will show all fields with value.

I have checked again and removed:

{{ conversations.getUser().getUsername() }}

Now user in

{{ dump(conversations) }}

Is again "null".

Okay, all works, but can someone explain me, why symfony works like this???

Thanks!!

Stephan B
  • 21
  • 3