-
I created a function which is supposed to create postgres jsonb "Do any of the strings in the text array exist as top-level keys or array elements?" syntax Generated code:
Complete query: SELECT "users".*
FROM "ori"."users" AS "users"
LEFT JOIN "ori"."user_roles" AS "user_roles" ON ("users"."id" = "user_roles"."user_id")
LEFT JOIN "ori"."roles" AS "roles" ON ("user_roles"."role_id" = "roles"."id")
WHERE "roles"."privileges" ?| array ['*', 'app', 'app.client', 'app.client.entry']
GROUP BY "users"."id"; Function: use Nextras\Orm\Collection\Functions\BaseCompareFunction;
use Nextras\Orm\Collection\Helpers\DbalExpressionResult;
final class JsonAnyKeyOrValueExistsFunction extends BaseCompareFunction
{
protected function evaluateInPhp($sourceValue, $targetValue): bool
{
// TODO: Implement evaluateInPhp() method.
}
protected function evaluateInDb(DbalExpressionResult $expression, $value): DbalExpressionResult
{
assert(is_array($value));
$inlineValue = '';
$last = array_key_last($value);
foreach ($value as $key => $item) {
$item = substr($item, 1, -1);
$inlineValue .= "'{$item}'";
if ($key !== $last) {
$inlineValue .= ', ';
}
}
$inlineValue = "array[{$inlineValue}]";
return $expression->append('?| %raw', $inlineValue);
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
So far I found value is wrapped in And the |
Beta Was this translation helpful? Give feedback.
-
This is rather a Dbal question. There are two possible solution:
protected function evaluateInDb(DbalExpressionResult $expression, $value): DbalExpressionResult
{
assert(is_array($value));
$size = count($value);
$placeholders = implode(', ', array_fill(0, $size, '%s'));
$arrayExpression = "array[$placeholders]";
return $expression->append("?| %arrayExpression", array_values($value));
} |
Beta Was this translation helpful? Give feedback.
This is rather a Dbal question. There are two possible solution: