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

Add expressionToString to getOtherClauses #451

Merged
merged 2 commits into from
Dec 23, 2023

Conversation

padre
Copy link
Contributor

@padre padre commented May 11, 2023

Fix exception: Object of class Illuminate\Database\Query\Expression could not be converted to string

Related: #441

Eloquent query that caused the bug, for example:


$this
            ->vigilances()
            ->has('investigations', '=', 1);

If i try to dump $where variable and got:

array:5 [▼ // vendor/genealabs/laravel-model-caching/src/CacheKey.php
  "type" => "Basic"
  "column" => Illuminate\Database\Query\Expression {#4958 ▼
    #value: "(select count(*) from `investigations` inner join `investigation_vigilance` on `investigations`.`id` = `investigation_vigilance`.`investigation_id` where `vigilances`.`id` = `investigation_vigilance`.`vigilance_id` and `investigations`.`deleted_at` is null) ◀"
  }
  "operator" => "="
  "value" => Illuminate\Database\Query\Expression {#4960 ▼
    #value: 1
  }
  "boolean" => "and"
]

Fix error: Object of class Illuminate\Database\Query\Expression could not be converted to string
@padre
Copy link
Contributor Author

padre commented Jun 30, 2023

@mikebronner can you merge this, please?

Thanks!

@mikebronner
Copy link
Owner

mikebronner commented Jun 30, 2023

@padre could you add a test that covers this specific use-case? I also made some small tweaks to the code.

@padre
Copy link
Contributor Author

padre commented Jul 4, 2023

@mikebronner Thank you very much for your quick response and your attention, but I don't think I have the necessary knowledge to add a test.

The exception occurs when I use ->has() in a belongsToMany relationship.

class Investigation extends Model
{
    public function vigilances()
    {
       return $this->belongsToMany(Vigilance::class);
    }
}

class Vigilance extends Model
{
    public function investigations()
    {
        return $this->belongsToMany(Investigation::class);
    }
}
$investigation
->vigilances()
->has('investigations', '=', 1)
->get();

This causes in the method "getOtherClauses(array $where)" in the statement

$column .= isset($where["column"]) ? $where["column"] : "";

the exception "Object of class IlluminateDatabaseQueryExpression could not be converted to string".

If i try to dump $where variable and got:

array:5 [
  "type" => "Basic"
  "column" => Illuminate\Database\Query\Expression {#3051 ▼
    #value: "(select count(*) from `investigations` inner join `investigation_vigilance` on `investigations`.`id` = `investigation_vigilance`.`investigation_id` where `vigilances`.`id` = `investigation_vigilance`.`vigilance_id` and `investigations`.`deleted_at` is null) ◀"
  }
  "operator" => "="
  "value" => Illuminate\Database\Query\Expression {#3053 ▼
    #value: 1
  }
  "boolean" => "and"

Thank you very much for your dedication.

@mikebronner mikebronner merged commit 650e20d into mikebronner:master Dec 23, 2023
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 this pull request may close these issues.

2 participants