Skip to content

Silence E_NOTICE warning: "Undefined index". #298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 16, 2013
Merged

Silence E_NOTICE warning: "Undefined index". #298

merged 1 commit into from
Sep 16, 2013

Conversation

fredrik-w
Copy link

Running PHP with error reporting on for E_NOTICE this bit me. Fix is simple and straightforward, check if key is defined and use that value if so, otherwise use null.

@doctrinebot
Copy link

Hello,

thank you for creating this pull request. I have automatically opened an issue
on our Jira Bug Tracker for you. See the issue link:

http://www.doctrine-project.org/jira/browse/DCOM-216

We use Jira to track the state of pull requests and the versions they got
included in.

@Ocramius
Copy link
Member

@fredrik-w the fix is invalid. A proxy expects ALL fields of the PK to be set.

Can you provide the case where this is happening? The problem is not the factory, and the notice is alright here (could be changed into an exception eventually, but only if really needed, since it's a performance sensitive part of the ORM/ODM).

@@ -118,7 +118,8 @@ public function getProxy($className, array $identifier)
$proxy = new $fqcn($definition->initializer, $definition->cloner);

foreach ($definition->identifierFields as $idField) {
$definition->reflectionFields[$idField]->setValue($proxy, $identifier[$idField]);
$value = isset($identifier[$idField]) ? $identifier[$idField] : null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

null is not a valid PK field value

@fredrik-w
Copy link
Author

Well that explains a bit more. For some reason (copy/paste monster perhaps) I had two columns defined as the PK when it should have been only one.

I feel like a proper exception thrown here would be better than an anonymous E_NOTICE. Can I just update my branch and replace the "fix" with throwing an OutOfBoundsException("Missing value for primary key {$idField} on {$className}");

@Ocramius
Copy link
Member

@fredrik-w can you modify the PR accordingly?

Adding a test is also necessary

@fredrik-w
Copy link
Author

I've updated the PR; now a proper exception is thrown if the value for a PK is missing.

@@ -118,6 +118,9 @@ public function getProxy($className, array $identifier)
$proxy = new $fqcn($definition->initializer, $definition->cloner);

foreach ($definition->identifierFields as $idField) {
if (!isset($identifier[$idField])) {
throw new \OutOfBoundsException("Missing value for primary key {$idField} on {$className}");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a new empty line after this line

@fredrik-w
Copy link
Author

New exception class added, fixed coding style as well.

Ocramius added a commit that referenced this pull request Sep 16, 2013
Silence E_NOTICE warning: "Undefined index".
@Ocramius Ocramius merged commit d9dea98 into doctrine:master Sep 16, 2013
@croensch
Copy link

Thanks, the error message is much clearer now. However if i pull back to 2.3.3, this data in my case is tolerated.

Suppose i have Entity A, B and C.

  • A's PK is id and has a many-to-one reference to B.
  • B's PK is composite of A.id and C.id and has another field some
  • C's PK is id

Now if i load A with the following DQL (actually without the where):
select * from A left join B where B.C is null

It will have half of the reference-identifier A but not C and still try to load it.

Ocramius added a commit that referenced this pull request Apr 21, 2014
Silence E_NOTICE warning: "Undefined index".
@jmikola
Copy link
Member

jmikola commented Jun 5, 2014

@Ocramius: This actually broke ODM's test suite recently (build 1236), since null is a valid identifier in MongoDB (we had some tests for references to documents with null IDs). Should this check have been implemented in ORM? I'm not sure about the other ODMs, but I imagine they may allow any PHP value to be used as an identifier as well.

Edit: Sorry for bumping an old thread (just realized this is nearly a year old). Looks like ODM picked it up since this was included in the v2.4.2 release.

jmikola added a commit to doctrine/mongodb-odm that referenced this pull request Jun 5, 2014
This test was originally added alongside a ReferenceMany test in d1b0ed3, but the latter was removed in a2c3ddd when @jwage added tests for non-scalar identifiers. The ReferenceOne test likely should have been removed at that point, too.

This fixes a build failure with Doctrine Common 2.4.2, due to doctrine/common#298. Therein, the common proxy classes no longer allow for null identifiers, even though they are technically valid in MongoDB.
@Ocramius
Copy link
Member

Ocramius commented Jun 5, 2014

Why is "not a value" an actual identifier? That's a conceptual mistake...
On 5 Jun 2014 17:50, "Jeremy Mikola" notifications@github.com wrote:

@Ocramius https://github.com/Ocramius: This actually broke ODM's test
suite recently (build 1236
https://travis-ci.org/doctrine/mongodb-odm/builds/26415417), since null
is a valid identifier in MongoDB (we had some tests for references to
documents with null IDs). Should this check have been implemented in ORM?
I'm not sure about the other ODMs, but I imagine they may allow any PHP
value to be used as an identifier as well.


Reply to this email directly or view it on GitHub
#298 (comment).

@jmikola
Copy link
Member

jmikola commented Jun 5, 2014

Filed under "not that you would but you could."

null is simply a value in the BSON spec, and any value other than array is usable as an identifier at the database level.

I just ended up deleting the test case, and I don't expect that any ODM users actually relied on this. I was just surprised to see this in a patch release, as throwing a new exception seemed like an API change more suited to a X.Y bump.

@Ocramius
Copy link
Member

Ocramius commented Jun 5, 2014

@jmikola it is weird that null is considered a value :|

null is null, heh...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants