DDC-2465: ProxyClass load, and empty ids #3093

Closed
opened 2026-01-22 14:12:01 +01:00 by admin · 10 comments
Owner

Originally created by @doctrinebot on GitHub (May 21, 2013).

Originally assigned to: @Ocramius on GitHub.

Jira issue originally created by user lele19871:

If you have lazy association (example many-to-one) that have empty id (for empty i mean empty string and not null) you (correctly) get EntityNotFoundException.

But i'm working with a system that sometimes put an empty string instead of null in fk fields.

So i've add this little snipped of code __load method in $_proxyClassTemplate, ProxyFactory:

// GT, 2013-05-21 If all identifier are empty you not need to try load

        if (is*array($this->*identifier)) {
            $isEmpty = true;
            foreach($this->_identifier as $iK => $iV) {
                if (!empty($iV))
                    $isEmpty = false;
            }

            if ($isEmpty)
                return;
        }
        // END GT EDIT
Originally created by @doctrinebot on GitHub (May 21, 2013). Originally assigned to: @Ocramius on GitHub. Jira issue originally created by user lele19871: If you have lazy association (example many-to-one) that have empty id (for empty i mean empty string and not null) you (correctly) get EntityNotFoundException. But i'm working with a system that sometimes put an empty string instead of null in fk fields. So i've add this little snipped of code __load method in $_proxyClassTemplate, ProxyFactory: ``` // GT, 2013-05-21 If all identifier are empty you not need to try load if (is*array($this->*identifier)) { $isEmpty = true; foreach($this->_identifier as $iK => $iV) { if (!empty($iV)) $isEmpty = false; } if ($isEmpty) return; } // END GT EDIT ```
admin added the Improvement label 2026-01-22 14:12:01 +01:00
admin closed this issue 2026-01-22 14:12:01 +01:00
Author
Owner

@doctrinebot commented on GitHub (May 21, 2013):

Comment created by @ocramius:

What does $this->_identifier contain in your failing case?

@doctrinebot commented on GitHub (May 21, 2013): Comment created by @ocramius: What does `$this->_identifier` contain in your failing case?
Author
Owner

@doctrinebot commented on GitHub (May 21, 2013):

Comment created by lele19871:

It contains for example

array(
[id] =>
);

Because on db you find an empty string and not a null value.

@doctrinebot commented on GitHub (May 21, 2013): Comment created by lele19871: It contains for example ``` array( [id] => ); ``` Because on db you find an empty string and not a null value.
Author
Owner

@doctrinebot commented on GitHub (May 21, 2013):

Comment created by @ocramius:

If I get it correctly, you are using empty strings to emulate NULL references, which is invalid in SQL ( I've explained it extensively at http://stackoverflow.com/questions/15502408/doctrine-2-use-default-0-values-instead-of-null-for-relation/15511715#15511715 )

Is this what you are doing? Because for any identifier that is not NULL an attempt to load it should be run, regardless of its content.

@doctrinebot commented on GitHub (May 21, 2013): Comment created by @ocramius: If I get it correctly, you are using empty strings to emulate NULL references, which is invalid in SQL ( I've explained it extensively at http://stackoverflow.com/questions/15502408/doctrine-2-use-default-0-values-instead-of-null-for-relation/15511715#15511715 ) Is this what you are doing? Because for any identifier that is not NULL an attempt to load it should be run, regardless of its content.
Author
Owner

@doctrinebot commented on GitHub (May 21, 2013):

Comment created by lele19871:

That's not me :)

It's the system by which i'm sharing the db.
It uses MyIsam engine with no fk or index, so i've found this way to skip load when on db i've empty strings.

Are there other (maybe cleaner) ways?

Thanks,
Gabriele

@doctrinebot commented on GitHub (May 21, 2013): Comment created by lele19871: That's not me :) It's the system by which i'm sharing the db. It uses MyIsam engine with no fk or index, so i've found this way to skip load when on db i've empty strings. Are there other (maybe cleaner) ways? Thanks, Gabriele
Author
Owner

@doctrinebot commented on GitHub (May 21, 2013):

Comment created by @ocramius:

No, the only correct way to handle this is to set NULL values for the association meta-columns. Marking as invalid

@doctrinebot commented on GitHub (May 21, 2013): Comment created by @ocramius: No, the only correct way to handle this is to set NULL values for the association meta-columns. Marking as invalid
Author
Owner

@doctrinebot commented on GitHub (May 21, 2013):

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (May 21, 2013): Issue was closed with resolution "Invalid"
Author
Owner

@doctrinebot commented on GitHub (May 21, 2013):

Comment created by lele19871:

I can't find doc about it: https://doctrine-orm.readthedocs.org/en/latest/reference/annotations-reference.html?highlight=column#annref-column

Can you give an example?

@doctrinebot commented on GitHub (May 21, 2013): Comment created by lele19871: I can't find doc about it: https://doctrine-orm.readthedocs.org/en/latest/reference/annotations-reference.html?highlight=column#annref-column Can you give an example?
Author
Owner

@doctrinebot commented on GitHub (May 21, 2013):

Comment created by @ocramius:

$this->someAssociation = NULL;. That's basically what I mean.

@doctrinebot commented on GitHub (May 21, 2013): Comment created by @ocramius: `$this->someAssociation = NULL;`. That's basically what I mean.
Author
Owner

@doctrinebot commented on GitHub (May 22, 2013):

Comment created by lele19871:

Mmm, didn't understand.

Anyway, we've tried to fix the old Delphi Monster witch store empty strings instead of nulls... with no results.

So, here is my workaround, maybe this can be useful for other developers who will be in troubles in the future..

In "UnitOfWork" class, public method "createEntity".

[...]

// TODO: Is this even computed right in all cases of composite keys?
                    foreach ($assoc['targetToSourceKeyColumns'] as $targetColumn => $srcColumn) {
                        $joinColumnValue = isset($data[$srcColumn]) ? $data[$srcColumn] : null;

                        // START-EDIT

                        // GT: our moas store empty string instead of null in fk columns.
                        // So, let's check and handle it as null!

                        if (empty($joinColumnValue))
                            $joinColumnValue = null;

                        // END-EDIT

                        if ($joinColumnValue !== null) {
                            if ($targetClass->containsForeignIdentifier) {
                                $associatedId[$targetClass->getFieldForColumn($targetColumn)] = $joinColumnValue;
                            } else {
                                $associatedId[$targetClass->fieldNames[$targetColumn]] = $joinColumnValue;
                            }
                        }
                    }

[...]

Regards,
Gabriele

@doctrinebot commented on GitHub (May 22, 2013): Comment created by lele19871: Mmm, didn't understand. Anyway, we've tried to fix the old Delphi Monster witch store empty strings instead of nulls... with no results. So, here is my workaround, maybe this can be useful for other developers who will be in troubles in the future.. In "UnitOfWork" class, public method "createEntity". [...] ``` // TODO: Is this even computed right in all cases of composite keys? foreach ($assoc['targetToSourceKeyColumns'] as $targetColumn => $srcColumn) { $joinColumnValue = isset($data[$srcColumn]) ? $data[$srcColumn] : null; // START-EDIT // GT: our moas store empty string instead of null in fk columns. // So, let's check and handle it as null! if (empty($joinColumnValue)) $joinColumnValue = null; // END-EDIT if ($joinColumnValue !== null) { if ($targetClass->containsForeignIdentifier) { $associatedId[$targetClass->getFieldForColumn($targetColumn)] = $joinColumnValue; } else { $associatedId[$targetClass->fieldNames[$targetColumn]] = $joinColumnValue; } } } ``` [...] Regards, Gabriele
Author
Owner

@doctrinebot commented on GitHub (May 22, 2013):

Comment created by @ocramius:

[~lele19871] the ORM does not deal with such architectures (nor with generally invalid usage of RDBMS systems). The only acceptable solution in ORM is with correct NULL values, as it should be, so this patch is invalid.

@doctrinebot commented on GitHub (May 22, 2013): Comment created by @ocramius: [~lele19871] the ORM does not deal with such architectures (nor with generally invalid usage of RDBMS systems). The only acceptable solution in ORM is with correct NULL values, as it should be, so this patch is invalid.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3093