'resolveMagicCall()' classify wrong? snake_case fields cannot be found #7222

Closed
opened 2026-01-22 15:47:16 +01:00 by admin · 1 comment
Owner

Originally created by @nsd4npo on GitHub (Sep 8, 2023).

Discussed in https://github.com/doctrine/orm/discussions/10938

Originally posted by nsd4npo September 8, 2023
Greetings,

I'm using a custom function within my defaultRepository extender class called findOneOrCreateByField($fieldName, $fieldValue). Here's a snippet of how it works:

$inflector = InflectorFactory::create()->build();
$field = $inflector->classify($field);
$finder = "findOneBy" . $field;
$entity = $this->$finder($fieldValue);

However, I encountered an issue when using it with the parameters testTime and \DateTime("now"), which resulted in a "field not found" error. After some debugging, I discovered the source of the problem in this (shortened) code block:

private function resolveMagicCall(string $method, string $by, array $arguments)
    $fieldName = lcfirst(self::$inflector->classify($by));

    if (!($this->_class->hasField($fieldName) || $this->_class->hasAssociation($fieldName))) {
        throw InvalidMagicMethodCall::becauseFieldNotFoundIn(

https://github.com/doctrine/orm/blob/2.16.x/lib/Doctrine/ORM/EntityRepository.php#L358-L360

In this context, the code uses classify on the field name, resulting in TestTime. The subsequent hasField check fails because it's looking for a test_time field instead of TestTime.

grafik

My conclusion is that for the hasField check, it should use snake_case or tableize, while for the hasAssociation check, using classify is appropriate.

Workaround in findOneOrCreateByField:

$field = $inflector->tableize($field);
$entity = $this->findOneBy([$field => $fieldValue]);
Originally created by @nsd4npo on GitHub (Sep 8, 2023). ### Discussed in https://github.com/doctrine/orm/discussions/10938 <div type='discussions-op-text'> <sup>Originally posted by **nsd4npo** September 8, 2023</sup> Greetings, I'm using a custom function within my `defaultRepository` extender class called `findOneOrCreateByField($fieldName, $fieldValue)`. Here's a snippet of how it works: ``` $inflector = InflectorFactory::create()->build(); $field = $inflector->classify($field); $finder = "findOneBy" . $field; $entity = $this->$finder($fieldValue); ``` However, I encountered an issue when using it with the parameters `testTime` and `\DateTime("now")`, which resulted in a "field not found" error. After some debugging, I discovered the source of the problem in this (shortened) code block: ``` private function resolveMagicCall(string $method, string $by, array $arguments) $fieldName = lcfirst(self::$inflector->classify($by)); if (!($this->_class->hasField($fieldName) || $this->_class->hasAssociation($fieldName))) { throw InvalidMagicMethodCall::becauseFieldNotFoundIn( ``` https://github.com/doctrine/orm/blob/2.16.x/lib/Doctrine/ORM/EntityRepository.php#L358-L360 In this context, the code uses classify on the field name, resulting in `TestTime`. The subsequent `hasField` check fails because it's looking for a `test_time` field instead of `TestTime`. ![grafik](https://github.com/doctrine/orm/assets/143995254/b5dbd732-26a3-41a2-afd1-391fda907eda) My conclusion is that for the `hasField` check, it should use snake_case or tableize, while for the `hasAssociation` check, using classify is appropriate. Workaround in findOneOrCreateByField: ``` $field = $inflector->tableize($field); $entity = $this->findOneBy([$field => $fieldValue]); ```
admin closed this issue 2026-01-22 15:47:17 +01:00
Author
Owner

@derrabus commented on GitHub (Sep 11, 2023):

We don't need two threads for the same topic. Duplicate of #10938

@derrabus commented on GitHub (Sep 11, 2023): We don't need two threads for the same topic. Duplicate of #10938
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7222