|
4 | 4 |
|
5 | 5 | namespace Doctrine\ORM\Persisters\Entity; |
6 | 6 |
|
7 | | -use BackedEnum; |
8 | 7 | use Doctrine\Common\Collections\Criteria; |
9 | 8 | use Doctrine\Common\Collections\Expr\Comparison; |
10 | 9 | use Doctrine\DBAL\Connection; |
|
26 | 25 | use Doctrine\ORM\Persisters\Exception\UnrecognizedField; |
27 | 26 | use Doctrine\ORM\Persisters\SqlExpressionVisitor; |
28 | 27 | use Doctrine\ORM\Persisters\SqlValueVisitor; |
29 | | -use Doctrine\ORM\Proxy\DefaultProxyClassNameResolver; |
30 | 28 | use Doctrine\ORM\Query; |
31 | | -use Doctrine\ORM\Query\QueryException; |
32 | 29 | use Doctrine\ORM\Repository\Exception\InvalidFindByCall; |
33 | 30 | use Doctrine\ORM\UnitOfWork; |
34 | 31 | use Doctrine\ORM\Utility\IdentifierFlattener; |
|
47 | 44 | use function count; |
48 | 45 | use function implode; |
49 | 46 | use function is_array; |
50 | | -use function is_object; |
51 | 47 | use function reset; |
52 | 48 | use function spl_object_id; |
53 | 49 | use function sprintf; |
@@ -394,7 +390,7 @@ final protected function extractIdentifierTypes(array $id, ClassMetadata $versio |
394 | 390 | $types = []; |
395 | 391 |
|
396 | 392 | foreach ($id as $field => $value) { |
397 | | - $types = array_merge($types, $this->getTypes($field, $value, $versionedClass)); |
| 393 | + $types = array_merge($types, PersisterHelper::inferParameterTypes($field, $value, $versionedClass, $this->em)); |
398 | 394 | } |
399 | 395 |
|
400 | 396 | return $types; |
@@ -955,8 +951,8 @@ public function expandCriteriaParameters(Criteria $criteria) |
955 | 951 | continue; |
956 | 952 | } |
957 | 953 |
|
958 | | - $sqlParams = array_merge($sqlParams, $this->getValues($value)); |
959 | | - $sqlTypes = array_merge($sqlTypes, $this->getTypes($field, $value, $this->class)); |
| 954 | + $sqlParams = array_merge($sqlParams, PersisterHelper::convertToParameterValue($value, $this->em)); |
| 955 | + $sqlTypes = array_merge($sqlTypes, PersisterHelper::inferParameterTypes($field, $value, $this->class, $this->em)); |
960 | 956 | } |
961 | 957 |
|
962 | 958 | return [$sqlParams, $sqlTypes]; |
@@ -1942,8 +1938,8 @@ public function expandParameters($criteria) |
1942 | 1938 | continue; // skip null values. |
1943 | 1939 | } |
1944 | 1940 |
|
1945 | | - $types = array_merge($types, $this->getTypes($field, $value, $this->class)); |
1946 | | - $params = array_merge($params, $this->getValues($value)); |
| 1941 | + $types = array_merge($types, PersisterHelper::inferParameterTypes($field, $value, $this->class, $this->em)); |
| 1942 | + $params = array_merge($params, PersisterHelper::convertToParameterValue($value, $this->em)); |
1947 | 1943 | } |
1948 | 1944 |
|
1949 | 1945 | return [$params, $types]; |
@@ -1971,127 +1967,13 @@ private function expandToManyParameters(array $criteria): array |
1971 | 1967 | continue; // skip null values. |
1972 | 1968 | } |
1973 | 1969 |
|
1974 | | - $types = array_merge($types, $this->getTypes($criterion['field'], $criterion['value'], $criterion['class'])); |
1975 | | - $params = array_merge($params, $this->getValues($criterion['value'])); |
| 1970 | + $types = array_merge($types, PersisterHelper::inferParameterTypes($criterion['field'], $criterion['value'], $criterion['class'], $this->em)); |
| 1971 | + $params = array_merge($params, PersisterHelper::convertToParameterValue($criterion['value'], $this->em)); |
1976 | 1972 | } |
1977 | 1973 |
|
1978 | 1974 | return [$params, $types]; |
1979 | 1975 | } |
1980 | 1976 |
|
1981 | | - /** |
1982 | | - * Infers field types to be used by parameter type casting. |
1983 | | - * |
1984 | | - * @param mixed $value |
1985 | | - * |
1986 | | - * @return int[]|null[]|string[] |
1987 | | - * @phpstan-return list<int|string|null> |
1988 | | - * |
1989 | | - * @throws QueryException |
1990 | | - */ |
1991 | | - private function getTypes(string $field, $value, ClassMetadata $class): array |
1992 | | - { |
1993 | | - $types = []; |
1994 | | - |
1995 | | - switch (true) { |
1996 | | - case isset($class->fieldMappings[$field]): |
1997 | | - $types = array_merge($types, [$class->fieldMappings[$field]['type']]); |
1998 | | - break; |
1999 | | - |
2000 | | - case isset($class->associationMappings[$field]): |
2001 | | - $assoc = $class->associationMappings[$field]; |
2002 | | - $class = $this->em->getClassMetadata($assoc['targetEntity']); |
2003 | | - |
2004 | | - if (! $assoc['isOwningSide']) { |
2005 | | - $assoc = $class->associationMappings[$assoc['mappedBy']]; |
2006 | | - $class = $this->em->getClassMetadata($assoc['targetEntity']); |
2007 | | - } |
2008 | | - |
2009 | | - $columns = $assoc['type'] === ClassMetadata::MANY_TO_MANY |
2010 | | - ? $assoc['relationToTargetKeyColumns'] |
2011 | | - : $assoc['sourceToTargetKeyColumns']; |
2012 | | - |
2013 | | - foreach ($columns as $column) { |
2014 | | - $types[] = PersisterHelper::getTypeOfColumn($column, $class, $this->em); |
2015 | | - } |
2016 | | - |
2017 | | - break; |
2018 | | - |
2019 | | - default: |
2020 | | - $types[] = null; |
2021 | | - break; |
2022 | | - } |
2023 | | - |
2024 | | - if (is_array($value)) { |
2025 | | - return array_map(static function ($type) { |
2026 | | - $type = Type::getType($type); |
2027 | | - |
2028 | | - return $type->getBindingType() + Connection::ARRAY_PARAM_OFFSET; |
2029 | | - }, $types); |
2030 | | - } |
2031 | | - |
2032 | | - return $types; |
2033 | | - } |
2034 | | - |
2035 | | - /** |
2036 | | - * Retrieves the parameters that identifies a value. |
2037 | | - * |
2038 | | - * @param mixed $value |
2039 | | - * |
2040 | | - * @return mixed[] |
2041 | | - */ |
2042 | | - private function getValues($value): array |
2043 | | - { |
2044 | | - if (is_array($value)) { |
2045 | | - $newValue = []; |
2046 | | - |
2047 | | - foreach ($value as $itemValue) { |
2048 | | - $newValue = array_merge($newValue, $this->getValues($itemValue)); |
2049 | | - } |
2050 | | - |
2051 | | - return [$newValue]; |
2052 | | - } |
2053 | | - |
2054 | | - return $this->getIndividualValue($value); |
2055 | | - } |
2056 | | - |
2057 | | - /** |
2058 | | - * Retrieves an individual parameter value. |
2059 | | - * |
2060 | | - * @param mixed $value |
2061 | | - * |
2062 | | - * @phpstan-return list<mixed> |
2063 | | - */ |
2064 | | - private function getIndividualValue($value): array |
2065 | | - { |
2066 | | - if (! is_object($value)) { |
2067 | | - return [$value]; |
2068 | | - } |
2069 | | - |
2070 | | - if ($value instanceof BackedEnum) { |
2071 | | - return [$value->value]; |
2072 | | - } |
2073 | | - |
2074 | | - $valueClass = DefaultProxyClassNameResolver::getClass($value); |
2075 | | - |
2076 | | - if ($this->em->getMetadataFactory()->isTransient($valueClass)) { |
2077 | | - return [$value]; |
2078 | | - } |
2079 | | - |
2080 | | - $class = $this->em->getClassMetadata($valueClass); |
2081 | | - |
2082 | | - if ($class->isIdentifierComposite) { |
2083 | | - $newValue = []; |
2084 | | - |
2085 | | - foreach ($class->getIdentifierValues($value) as $innerValue) { |
2086 | | - $newValue = array_merge($newValue, $this->getValues($innerValue)); |
2087 | | - } |
2088 | | - |
2089 | | - return $newValue; |
2090 | | - } |
2091 | | - |
2092 | | - return [$this->em->getUnitOfWork()->getSingleIdentifierValue($value)]; |
2093 | | - } |
2094 | | - |
2095 | 1977 | /** |
2096 | 1978 | * {@inheritDoc} |
2097 | 1979 | */ |
|
0 commit comments