Proxy returns default value without initializing #5382

Closed
opened 2026-01-22 15:06:00 +01:00 by admin · 7 comments
Owner

Originally created by @Wilt on GitHub (Jan 12, 2017).

If we define a default value like this:

<?php
/**
 * @Entity
 */
class MyEntity {
    /**
     * @var string
     *
     * @Column(name="myColumn", type="string", length="50")
     */
    private $myColumn = 'myDefaultValue';
    ...
}

And we have a Proxy of MyEntity instead of an initialized entity then pulling the proxy method returns a default value without initializing the entity and resolving the actual database value.

This means that the database value for myColumn is actually holding a completely different value we got returned from our proxy without us realizing this.

Since this way of defining default values is also mentioned in the documentation chapter: 3.2.2. How can I add default values to a column? we were very surprised that this issue occurred.

Are we doing something wrong here? Or is this a bug?

Originally created by @Wilt on GitHub (Jan 12, 2017). If we define a default value like this: <?php /** * @Entity */ class MyEntity { /** * @var string * * @Column(name="myColumn", type="string", length="50") */ private $myColumn = 'myDefaultValue'; ... } And we have a Proxy of `MyEntity` instead of an initialized entity then pulling the proxy method returns a default value without initializing the entity and resolving the actual database value. This means that the database value for `myColumn` is actually holding a completely different value we got returned from our proxy without us realizing this. Since this way of defining default values is also mentioned [in the documentation chapter: **3.2.2. How can I add default values to a column?**](http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/faq.html#how-can-i-add-default-values-to-a-column) we were very surprised that this issue occurred. Are we doing something wrong here? Or is this a bug?
admin added the BugMissing Tests labels 2026-01-22 15:06:00 +01:00
admin closed this issue 2026-01-22 15:06:00 +01:00
Author
Owner

@Ocramius commented on GitHub (Jan 12, 2017):

@Wilt what code did you use to generate, retrieve and read the value from the proxy?

@Ocramius commented on GitHub (Jan 12, 2017): @Wilt what code did you use to generate, retrieve and read the value from the proxy?
Author
Owner

@Wilt commented on GitHub (Jan 12, 2017):

I am just checking once more....
I realize now that in this particular case I might have not used the getter but accessed the property directly. I will close and reopen if I am sure.
I apologize...

@Wilt commented on GitHub (Jan 12, 2017): I am just checking once more.... I realize now that in this particular case I might have not used the `getter` but accessed the property directly. I will close and reopen if I am sure. I apologize...
Author
Owner

@Ocramius commented on GitHub (Jan 12, 2017):

@Wilt accessing the property directly, if the property is public, should still trigger lazy-loading. Can you check that, please?

@Ocramius commented on GitHub (Jan 12, 2017): @Wilt accessing the property directly, if the property is `public`, should still trigger lazy-loading. Can you check that, please?
Author
Owner

@Wilt commented on GitHub (Jan 12, 2017):

@Ocramius Yes, I got very confused and had a slight panic attack 😬

But it seems we are using a getter anyway, so I am not sure why it doesn't work as expected.
I need to leave now but tomorrow I will have a more in depth debug session and then I will report back to you. Thanks for your interest in the issue. Have a great evening/day (whatever timezone you are in).

@Wilt commented on GitHub (Jan 12, 2017): @Ocramius Yes, I got very confused and had a slight panic attack :grimacing: But it seems we are using a getter anyway, so I am not sure why it doesn't work as expected. I need to leave now but tomorrow I will have a more in depth debug session and then I will report back to you. Thanks for your interest in the issue. Have a great evening/day (whatever timezone you are in).
Author
Owner

@Ocramius commented on GitHub (Jan 12, 2017):

Reopening for you to provide feedback then :-)

@Ocramius commented on GitHub (Jan 12, 2017): Reopening for you to provide feedback then :-)
Author
Owner

@Wilt commented on GitHub (Jan 13, 2017):

We can't reproduce this issue. Probably a bad from our side.
I will close.
Thanks for your time and interest.

@Wilt commented on GitHub (Jan 13, 2017): We can't reproduce this issue. Probably a bad from our side. I will close. Thanks for your time and interest.
Author
Owner

@Gappa commented on GitHub (Sep 9, 2021):

Google sent me here, I had the same problem:

  • Developer mode, proxies are generated on demand (but not updated).
  • An entity with boolean property/column $isPricelist with the default value of false.
  • $entity->isPricelist() kept returning false even for cases when it should have been true.
  • The problem was somewhere in the generated proxy file.

Sadly, I was too quick with purging of the proxies directory, so I can't tell what was the exact problem. But I got the same behavior by removing all mentions of isPricelist from the proxy class, so the getter calls the parent method directly without triggering an actual DB query for the real data.

TLDR: The proxy file for the entity was outdated and needed to be refreshed.

@Gappa commented on GitHub (Sep 9, 2021): Google sent me here, I had the same problem: - Developer mode, proxies are generated on demand (but not updated). - An entity with boolean property/column `$isPricelist` with the default value of `false`. - `$entity->isPricelist()` kept returning `false` even for cases when it should have been `true`. - The problem was somewhere in the generated proxy file. Sadly, I was too quick with purging of the proxies directory, so I can't tell what was the exact problem. But I got the same behavior by removing all mentions of `isPricelist` from the proxy class, so the getter calls the parent method directly without triggering an actual DB query for the real data. TLDR: The proxy file for the entity was outdated and needed to be refreshed.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5382