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

Reuse of custom filter within the same query will result in a join error #43

Open
sjorskamphuis opened this issue Oct 8, 2018 · 1 comment

Comments

@sjorskamphuis
Copy link

sjorskamphuis commented Oct 8, 2018

When using the same custom filter multiple times, the join statement will be appended twice to the query builder.

Example of the custom filter:

/**
     * @param Builder $query
     * @param         $method
     * @param         $clauseOperator
     * @param         $value
     *
     * @return Builder
     */
    public function filterStartLocation(Builder $query, $method, $clauseOperator, $value): Builder
    {
        // if clauseOperator is identical to false,
        // we are using a specific SQL method in its place (e.g. `in`, `between`)
        if ($clauseOperator === false) {
            $query->$method('locations.location_type_id', $value);
        } else {
            $query->$method('locations.location_type_id', $clauseOperator, $value);
        }

        return $query;
    }

As you can see, the custom filter will check the location_type within the location. the startLocation is an eloquent relation defined in the Model.

An error will occur when using this filter more then once within the same query:

The output of the Builder is:

Builder {#431 ▼
  #query: Builder {#417 ▼
    +connection: MySqlConnection {#128 ▶}
    +grammar: MySqlGrammar {#129 ▶}
    +processor: MySqlProcessor {#130}
    +bindings: array:7 [▶]
    +aggregate: null
    +columns: array:1 [▶]
    +distinct: false
    +from: "schedules"
    +joins: array:2 [▼
      0 => JoinClause {#877 ▼
        +type: "inner"
        +table: "locations"
        -parentQuery: Builder {#417}
        +connection: MySqlConnection {#128 ▶}
        +grammar: MySqlGrammar {#129 ▶}
        +processor: MySqlProcessor {#130}
        +bindings: array:7 [▶]
        +aggregate: null
        +columns: null
        +distinct: false
        +from: null
        +joins: null
        +wheres: array:1 [▼
          0 => array:5 [▼
            "type" => "Column"
            "first" => "schedules.end_location_id"
            "operator" => "="
            "second" => "locations.id"
            "boolean" => "and"
          ]
        ]
        +groups: null
        +havings: null
        +orders: null
        +limit: null
        +offset: null
        +unions: null
        +unionLimit: null
        +unionOffset: null
        +unionOrders: null
        +lock: null
        +operators: array:29 [▶]
        +useWritePdo: false
      }
      1 => JoinClause {#878 ▼
        +type: "inner"
        +table: "locations"
        -parentQuery: Builder {#417}
        +connection: MySqlConnection {#128 ▶}
        +grammar: MySqlGrammar {#129 ▶}
        +processor: MySqlProcessor {#130}
        +bindings: array:7 [▶]
        +aggregate: null
        +columns: null
        +distinct: false
        +from: null
        +joins: null
        +wheres: array:1 [▼
          0 => array:5 [▼
            "type" => "Column"
            "first" => "schedules.end_location_id"
            "operator" => "="
            "second" => "locations.id"
            "boolean" => "and"
          ]
        ]
        +groups: null
        +havings: null
        +orders: null
        +limit: null
        +offset: null
        +unions: null
        +unionLimit: null
        +unionOffset: null
        +unionOrders: null
        +lock: null
        +operators: array:29 [▶]
        +useWritePdo: false
      }
    ]
    +wheres: array:2 [▼
      0 => array:3 [▼
        "type" => "Nested"
        "query" => Builder {#886 ▼
          +connection: MySqlConnection {#128 ▶}
          +grammar: MySqlGrammar {#129 ▶}
          +processor: MySqlProcessor {#130}
          +bindings: array:7 [▶]
          +aggregate: null
          +columns: null
          +distinct: false
          +from: "schedules"
          +joins: null
          +wheres: array:1 [▼
            0 => array:5 [▼
              "type" => "Basic"
              "column" => "locations.location_type_id"
              "operator" => "="
              "value" => array:1 [▶]
              "boolean" => "and"
            ]
          ]
          +groups: null
          +havings: null
          +orders: null
          +limit: null
          +offset: null
          +unions: null
          +unionLimit: null
          +unionOffset: null
          +unionOrders: null
          +lock: null
          +operators: array:29 [▶]
          +useWritePdo: false
        }
        "boolean" => "and"
      ]
      1 => array:3 [▼
        "type" => "Nested"
        "query" => Builder {#871 ▼
          +connection: MySqlConnection {#128 ▶}
          +grammar: MySqlGrammar {#129 ▶}
          +processor: MySqlProcessor {#130}
          +bindings: array:7 [▶]
          +aggregate: null
          +columns: null
          +distinct: false
          +from: "schedules"
          +joins: null
          +wheres: array:1 [▼
            0 => array:5 [▼
              "type" => "Basic"
              "column" => "locations.location_type_id"
              "operator" => "="
              "value" => "8"
              "boolean" => "and"
            ]
          ]
          +groups: null
          +havings: null
          +orders: null
          +limit: null
          +offset: null
          +unions: null
          +unionLimit: null
          +unionOffset: null
          +unionOrders: null
          +lock: null
          +operators: array:29 [▶]
          +useWritePdo: false
        }
        "boolean" => "and"
      ]
    ]
    +groups: null
    +havings: null
    +orders: null
    +limit: null
    +offset: null
    +unions: null
    +unionLimit: null
    +unionOffset: null
    +unionOrders: null
    +lock: null
    +operators: array:29 [▶]
    +useWritePdo: false
  }
  #model: Schedule {#368 ▶}
  #eagerLoad: []
  #localMacros: []
  #onDelete: null
  #passthru: array:13 [▶]
  #scopes: []
  #removedScopes: []
}

Is there a workaround available for this issue?

--
Using Homestead with environment versions:

PHP:  7.2
Laravel: 5.6.39
@granith
Copy link

granith commented Jan 11, 2019

@sjors-k-nl did you find any workaround, I'm having same issue?

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 a pull request may close this issue.

2 participants