Skip to content
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

Impossible to unset a field when the type is object_id #2537

Open
olivier34000 opened this issue Jun 15, 2023 · 1 comment
Open

Impossible to unset a field when the type is object_id #2537

olivier34000 opened this issue Jun 15, 2023 · 1 comment
Labels

Comments

@olivier34000
Copy link

BC Break Report

Q A
BC Break yes
Version 2.1

Summary

I have a document with a field with the type object_id. I try to unset this field with the query builder and a exception is thrown.

Previous behavior

In the 1.3 version it worked but I think because the driver was less strict.

Current behavior

How to reproduce

Add a field in the document User

+    /**
+     * @ODM\Field(type="object_id")
+     *
+     * @var string|null
+     */
+    protected $identifier;
+

Now we can add test in the class BuilderTest

+    public function testUnset2Field(): void
+    {
+        $qb = $this->getTestQueryBuilder()
+            ->updateOne()
+            ->field('identifier')->unsetField()
+            ->field('username')->equals('boo');
+
+        $expected = ['username' => 'boo'];
+        self::assertEquals($expected, $qb->getQueryArray());
+
+        $expected = [
+            '$unset' => ['identifier' => 1],
+        ];
+        self::assertEquals($expected, $qb->getNewObj());
+    }

The error

1) Doctrine\ODM\MongoDB\Tests\Query\BuilderTest::testUnset2Field                                          
TypeError: MongoDB\BSON\ObjectId::__construct(): Argument #1 ($id) must be of type ?string, int given     
@malarzm malarzm added the Bug label Jun 15, 2023
@malarzm malarzm added this to the 2.5.3 milestone Jun 15, 2023
@malarzm
Copy link
Member

malarzm commented Jun 15, 2023

Thanks for the report! Scheduling for the next bugfix version.

I believe the bug lies somewhere in \Doctrine\ODM\MongoDB\Persisters\DocumentPersister::prepareQueryOrNewObj as ODM tries to prepare a query, sees identifier field and tries to case it to its type.

@malarzm malarzm modified the milestones: 2.5.3, 2.5.4 Oct 23, 2023
@malarzm malarzm modified the milestones: 2.5.4, 2.5.5 Nov 3, 2023
@alcaeus alcaeus removed this from the 2.5.5 milestone Nov 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants