ramsey/uuid-doctrine

Binary not working with Doctrine Tree extension

pdugas opened this issue · 4 comments

I have a Symfony project with a Doctrine entity that's using the Doctrine Tree Extension. It looks something like below. Note the use of the "uuid_binary_ordered_time" column type which is setup per the docs.

/**
 * @ORM\Entity
 * @Gedmo\Tree(type="nested")
 */
class Foo
{
    **
     * @ORM\Id
     * @ORM\Column(type="uuid_binary_ordered_time", unique=true)
     */
    protected $id;

    /**
     * @Gedmo\TreeParent
     * @ORM\ManyToOne(targetEntity="Foo", inversedBy="children")
     */
    private $parent = null;

    /**
     * @ORM\OneToMany(targetEntity="Foo", mappedBy="parent")
     */
    private $children;
    
    /**
     * @ORM\ManyToOne(targetEntity="Foo")
     */
    private $root;

    /**
     * @Gedmo\TreeLeft
     * @ORM\Column(type="integer")
     */
    private $lft = null;

    /**
     * @Gedmo\TreeRight
     * @ORM\Column(type="integer")
     */
    private $rgt = null;

    __construct()
    {
        $this->id = Uuid\Uuid::uuid1();
        $this->children = new ArrayCollection();
    }
}

My test is simply persisting a single entity with no parent or children. I am seeing it insert the record setting the id value to the generated UUID and $parent to NULL both as expected.

[2020-03-06 23:40:38] doctrine.DEBUG: INSERT INTO foo (lft, rgt, name, id) VALUES (?, ?, ?, ?) {"1":0,"2":0,"3":"EG","4":"[object] (Ramsey\\Uuid\\Uuid: \"cb3bd9a0-602d-11ea-bf3f-0242be8c6bde\")"} []

The INSERT is immediately followed by an UPDATE to set $root, $left, and $right.

[2020-03-06 23:40:38] doctrine.DEBUG: UPDATE foo SET root_id = ?, lft = 1, rgt = 2 WHERE id = ? ["[object] (Ramsey\\Uuid\\Uuid: \"cb3bfcb4-602d-11ea-982c-0242be8c6bde\")","[object] (Ramsey\\Uuid\\Uuid: \"cb3bfcb4-602d-11ea-982c-0242be8c6bde\")"] []

Trouble is, that UPDATE appears to match no existing records so the $root, $left, and $right values for the inserted record are wrong. (the tree extension's listener is not checking that effected-rows result so it proceeds without error.

Curiously, if I switch back to normal "uuid" as the column type, it works. If fails with "uuid_binary" too. I feel like I'm missing something obvious but have been staring at this for the better part of the day and don't see the issue. Is this a bug?

https://github.com/Atlantic18/DoctrineExtensions/blob/v2.4.x/lib/Gedmo/Tree/Strategy/ORM/Nested.php#L525

That's the query in the Doctrine Tree extension that's returning zero matches when it should return 1. I wonder if there's something like a double text/binary conversion happening or something odd like that.

Enabled the query log in MariaDB to get the exact queries.

		    41 Query	INSERT INTO foo (lft, rgt, name, id) VALUES (0, 0, 'EG', '�`�Raִ	B��k�')
		    41 Query	UPDATE foo SET root_id = '1d5261d6-608e-11ea-b409-0242be8c6bde', lft = 1, rgt = 2 WHERE id = '1d5261d6-608e-11ea-b409-0242be8c6bde'

So, there's the rub. It's not converting the UUIDs to binary values.

The issue here can be resolved with a fix in the DoctrineExtensions package. Closing this.