DDC-2313: Deep clone for DBAL QueryBuilder #2906

Closed
opened 2026-01-22 14:07:05 +01:00 by admin · 5 comments
Owner

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

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user tcm:

This is basically a duplicate of another issue I stumbled across lately but cannot find here again. It added a **clone() function to the ORM QueryBuilder to allow this use case:
Create a base query and derive two different queries from it.

I adopted the code for the DBAL QueryBuilder which is suffering the same issue (e.g. expressions were not cloned but shared between instances). The code is tested at least for my limited use case.

    /****
     * Deep clone of all expression objects in the SQL parts.
     *
     * @return void
     */
    public function **clone()
    {
        foreach ($this->sqlParts as $part => $elements) {
            if (is_array($this->sqlParts[$part])) {
                foreach ($this->sqlParts[$part] as $idx => $element) {
                    if (is_object($element)) {
                        $this->sqlParts[$part][$idx] = clone $element;
                    }
                }
            } else if (is_object($elements)) {
                $this->sqlParts[$part] = clone $elements;
            }
        }

        $params = array();

        foreach ($this->params as $param) {
            $params[] = clone $param;
        }

        $this->params = $params;
    }
Originally created by @doctrinebot on GitHub (Feb 21, 2013). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user tcm: This is basically a duplicate of another issue I stumbled across lately but cannot find here again. It added a **clone() function to the ORM QueryBuilder to allow this use case: Create a base query and derive two different queries from it. I adopted the code for the DBAL QueryBuilder which is suffering the same issue (e.g. expressions were not cloned but shared between instances). The code is tested at least for my limited use case. ``` /**** * Deep clone of all expression objects in the SQL parts. * * @return void */ public function **clone() { foreach ($this->sqlParts as $part => $elements) { if (is_array($this->sqlParts[$part])) { foreach ($this->sqlParts[$part] as $idx => $element) { if (is_object($element)) { $this->sqlParts[$part][$idx] = clone $element; } } } else if (is_object($elements)) { $this->sqlParts[$part] = clone $elements; } } $params = array(); foreach ($this->params as $param) { $params[] = clone $param; } $this->params = $params; } ```
admin added the Improvement label 2026-01-22 14:07:05 +01:00
admin closed this issue 2026-01-22 14:07:06 +01:00
Author
Owner

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

@doctrinebot commented on GitHub (Feb 21, 2013): - is duplicated by [DBAL-537: [GH-330] Cloning](http://www.doctrine-project.org/jira/browse/DBAL-537)
Author
Owner

@doctrinebot commented on GitHub (Jun 3, 2013):

Comment created by tcm:

Thanks for adding the code tags. Just updated my doctrine to 2.4-RC1 and did not find this in there. Would be nice to see this in the new version. Any obstacles I could be of help with?

@doctrinebot commented on GitHub (Jun 3, 2013): Comment created by tcm: Thanks for adding the code tags. Just updated my doctrine to 2.4-RC1 and did not find this in there. Would be nice to see this in the new version. Any obstacles I could be of help with?
Author
Owner

@doctrinebot commented on GitHub (Jun 3, 2013):

Comment created by @ocramius:

[~tcm], there's an open PR at https://github.com/doctrine/dbal/pull/327

@doctrinebot commented on GitHub (Jun 3, 2013): Comment created by @ocramius: [~tcm], there's an open PR at https://github.com/doctrine/dbal/pull/327
Author
Owner

@doctrinebot commented on GitHub (Jun 18, 2013):

Comment created by @beberlei:

Fixed in 2.4

@doctrinebot commented on GitHub (Jun 18, 2013): Comment created by @beberlei: Fixed in 2.4
Author
Owner

@doctrinebot commented on GitHub (Jun 18, 2013):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (Jun 18, 2013): Issue was closed with resolution "Fixed"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#2906