Skip to content

Add ArrayType as a generic wrapper for a "list of custom types" #6883

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

Closed

Conversation

mpdude
Copy link
Contributor

@mpdude mpdude commented Mar 31, 2025

Q A
Type feature
Fixed issues

Summary

The list of parameters conversion is currently limited to a few types expressed by the ArrayParameterType enum.

In doctrine/orm#11897, it might be necessary to express "list of parameters of a given custom type".

As suggested in doctrine/orm#11897 (comment), this PR is an initial exploration of using a new ArrayType to be wrapped around any other underlying type.

@mpdude mpdude marked this pull request as draft March 31, 2025 20:54
@@ -449,7 +450,7 @@ public function getParameterTypes(): array
*
* @param int|string $key The key of the bound parameter type
*/
public function getParameterType(int|string $key): string|ParameterType|Type|ArrayParameterType
public function getParameterType(int|string $key): string|ParameterType|Type|ArrayParameterType|ArrayType
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BC break?

Copy link
Member

@morozov morozov Mar 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My idea was that ArrayType is a Type. It with throw a "not implemented" exception for the SQL declaration (because we can't use it as a column type) but should be able to implement the rest of the methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would it do to convert a PHP value (an array, probably?) to a database value?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will delegate the conversion of each element of the array to its element type. We can start simple and accept only Type in the constructor to prove the idea:

public function __construct(private readonly Type $elementType)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When would it be necessary to use the ArrayType in this way?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When building the query here. If this code builds the parameter types as [new ArrayType('rot13'), 'rot13'], the DBAL should be able to apply rot13 to the array elements similar to how it does with a single value.

@morozov
Copy link
Member

morozov commented May 23, 2025

After taking a closer look at this, I don't think ArrayType() parameterized with the element type is the right approach. Spoiler alert: even binding a parameter as array in the QueryBuilder::setParameter() or Connection::executeQuery() is the right approach.

Currently, we have two major type systems:

  1. Type – represents the type of the entity field and maps to the type of the database column. It can be used in schema definition and in SQL queries.
  2. ParameterType – represents the binding type of a variable to an SQL query. It can be used in all sorts of SQL (SELECT, INSERT, UPDATE, etc.)

Also, we have ArrayParameterType, which is sort of a snowflake. All APIs that accept the above two type systems also accept ArrayParameterType, but it only makes sense if used with the IN SQL clause. Therefore, it shouldn't be accepted by all the APIs that accept Type|ParameterType, it should be only accepted by some sort of query builder that accepts an array of values and the element type and returns SQL, parameters and parameter types (a component that can be represented by the Query class.

Any attempt to define the expansion of an array of values to the IN clause by using the existing API will be forcing a square peg into a round hole.

@morozov morozov closed this May 23, 2025
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