|
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; |
@@ -392,7 +388,7 @@ final protected function extractIdentifierTypes(array $id, ClassMetadata $versio |
392 | 388 | $types = []; |
393 | 389 |
|
394 | 390 | foreach ($id as $field => $value) { |
395 | | - $types = array_merge($types, $this->getTypes($field, $value, $versionedClass)); |
| 391 | + $types = array_merge($types, PersisterHelper::inferParameterTypes($field, $value, $versionedClass, $this->em)); |
396 | 392 | } |
397 | 393 |
|
398 | 394 | return $types; |
@@ -953,8 +949,8 @@ public function expandCriteriaParameters(Criteria $criteria) |
953 | 949 | continue; |
954 | 950 | } |
955 | 951 |
|
956 | | - $sqlParams = array_merge($sqlParams, $this->getValues($value)); |
957 | | - $sqlTypes = array_merge($sqlTypes, $this->getTypes($field, $value, $this->class)); |
| 952 | + $sqlParams = array_merge($sqlParams, PersisterHelper::convertToParameterValue($value, $this->em)); |
| 953 | + $sqlTypes = array_merge($sqlTypes, PersisterHelper::inferParameterTypes($field, $value, $this->class, $this->em)); |
958 | 954 | } |
959 | 955 |
|
960 | 956 | return [$sqlParams, $sqlTypes]; |
@@ -1947,8 +1943,8 @@ public function expandParameters($criteria) |
1947 | 1943 | continue; // skip null values. |
1948 | 1944 | } |
1949 | 1945 |
|
1950 | | - $types = array_merge($types, $this->getTypes($field, $value, $this->class)); |
1951 | | - $params = array_merge($params, $this->getValues($value)); |
| 1946 | + $types = array_merge($types, PersisterHelper::inferParameterTypes($field, $value, $this->class, $this->em)); |
| 1947 | + $params = array_merge($params, PersisterHelper::convertToParameterValue($value, $this->em)); |
1952 | 1948 | } |
1953 | 1949 |
|
1954 | 1950 | return [$params, $types]; |
@@ -1976,127 +1972,13 @@ private function expandToManyParameters(array $criteria): array |
1976 | 1972 | continue; // skip null values. |
1977 | 1973 | } |
1978 | 1974 |
|
1979 | | - $types = array_merge($types, $this->getTypes($criterion['field'], $criterion['value'], $criterion['class'])); |
1980 | | - $params = array_merge($params, $this->getValues($criterion['value'])); |
| 1975 | + $types = array_merge($types, PersisterHelper::inferParameterTypes($criterion['field'], $criterion['value'], $criterion['class'], $this->em)); |
| 1976 | + $params = array_merge($params, PersisterHelper::convertToParameterValue($criterion['value'], $this->em)); |
1981 | 1977 | } |
1982 | 1978 |
|
1983 | 1979 | return [$params, $types]; |
1984 | 1980 | } |
1985 | 1981 |
|
1986 | | - /** |
1987 | | - * Infers field types to be used by parameter type casting. |
1988 | | - * |
1989 | | - * @param mixed $value |
1990 | | - * |
1991 | | - * @return int[]|null[]|string[] |
1992 | | - * @phpstan-return list<int|string|null> |
1993 | | - * |
1994 | | - * @throws QueryException |
1995 | | - */ |
1996 | | - private function getTypes(string $field, $value, ClassMetadata $class): array |
1997 | | - { |
1998 | | - $types = []; |
1999 | | - |
2000 | | - switch (true) { |
2001 | | - case isset($class->fieldMappings[$field]): |
2002 | | - $types = array_merge($types, [$class->fieldMappings[$field]['type']]); |
2003 | | - break; |
2004 | | - |
2005 | | - case isset($class->associationMappings[$field]): |
2006 | | - $assoc = $class->associationMappings[$field]; |
2007 | | - $class = $this->em->getClassMetadata($assoc['targetEntity']); |
2008 | | - |
2009 | | - if (! $assoc['isOwningSide']) { |
2010 | | - $assoc = $class->associationMappings[$assoc['mappedBy']]; |
2011 | | - $class = $this->em->getClassMetadata($assoc['targetEntity']); |
2012 | | - } |
2013 | | - |
2014 | | - $columns = $assoc['type'] === ClassMetadata::MANY_TO_MANY |
2015 | | - ? $assoc['relationToTargetKeyColumns'] |
2016 | | - : $assoc['sourceToTargetKeyColumns']; |
2017 | | - |
2018 | | - foreach ($columns as $column) { |
2019 | | - $types[] = PersisterHelper::getTypeOfColumn($column, $class, $this->em); |
2020 | | - } |
2021 | | - |
2022 | | - break; |
2023 | | - |
2024 | | - default: |
2025 | | - $types[] = null; |
2026 | | - break; |
2027 | | - } |
2028 | | - |
2029 | | - if (is_array($value)) { |
2030 | | - return array_map(static function ($type) { |
2031 | | - $type = Type::getType($type); |
2032 | | - |
2033 | | - return $type->getBindingType() + Connection::ARRAY_PARAM_OFFSET; |
2034 | | - }, $types); |
2035 | | - } |
2036 | | - |
2037 | | - return $types; |
2038 | | - } |
2039 | | - |
2040 | | - /** |
2041 | | - * Retrieves the parameters that identifies a value. |
2042 | | - * |
2043 | | - * @param mixed $value |
2044 | | - * |
2045 | | - * @return mixed[] |
2046 | | - */ |
2047 | | - private function getValues($value): array |
2048 | | - { |
2049 | | - if (is_array($value)) { |
2050 | | - $newValue = []; |
2051 | | - |
2052 | | - foreach ($value as $itemValue) { |
2053 | | - $newValue = array_merge($newValue, $this->getValues($itemValue)); |
2054 | | - } |
2055 | | - |
2056 | | - return [$newValue]; |
2057 | | - } |
2058 | | - |
2059 | | - return $this->getIndividualValue($value); |
2060 | | - } |
2061 | | - |
2062 | | - /** |
2063 | | - * Retrieves an individual parameter value. |
2064 | | - * |
2065 | | - * @param mixed $value |
2066 | | - * |
2067 | | - * @phpstan-return list<mixed> |
2068 | | - */ |
2069 | | - private function getIndividualValue($value): array |
2070 | | - { |
2071 | | - if (! is_object($value)) { |
2072 | | - return [$value]; |
2073 | | - } |
2074 | | - |
2075 | | - if ($value instanceof BackedEnum) { |
2076 | | - return [$value->value]; |
2077 | | - } |
2078 | | - |
2079 | | - $valueClass = DefaultProxyClassNameResolver::getClass($value); |
2080 | | - |
2081 | | - if ($this->em->getMetadataFactory()->isTransient($valueClass)) { |
2082 | | - return [$value]; |
2083 | | - } |
2084 | | - |
2085 | | - $class = $this->em->getClassMetadata($valueClass); |
2086 | | - |
2087 | | - if ($class->isIdentifierComposite) { |
2088 | | - $newValue = []; |
2089 | | - |
2090 | | - foreach ($class->getIdentifierValues($value) as $innerValue) { |
2091 | | - $newValue = array_merge($newValue, $this->getValues($innerValue)); |
2092 | | - } |
2093 | | - |
2094 | | - return $newValue; |
2095 | | - } |
2096 | | - |
2097 | | - return [$this->em->getUnitOfWork()->getSingleIdentifierValue($value)]; |
2098 | | - } |
2099 | | - |
2100 | 1982 | /** |
2101 | 1983 | * {@inheritDoc} |
2102 | 1984 | */ |
|
0 commit comments