|
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\Common\Collections\Order; |
|
31 | 30 | use Doctrine\ORM\Persisters\Exception\UnrecognizedField; |
32 | 31 | use Doctrine\ORM\Persisters\SqlExpressionVisitor; |
33 | 32 | use Doctrine\ORM\Persisters\SqlValueVisitor; |
34 | | -use Doctrine\ORM\Proxy\DefaultProxyClassNameResolver; |
35 | 33 | use Doctrine\ORM\Query; |
36 | | -use Doctrine\ORM\Query\QueryException; |
37 | 34 | use Doctrine\ORM\Query\ResultSetMapping; |
38 | 35 | use Doctrine\ORM\Repository\Exception\InvalidFindByCall; |
39 | 36 | use Doctrine\ORM\UnitOfWork; |
|
45 | 42 | use function array_combine; |
46 | 43 | use function array_keys; |
47 | 44 | use function array_map; |
48 | | -use function array_merge; |
49 | 45 | use function array_search; |
50 | 46 | use function array_unique; |
51 | 47 | use function array_values; |
52 | 48 | use function assert; |
53 | 49 | use function count; |
54 | 50 | use function implode; |
55 | 51 | use function is_array; |
56 | | -use function is_object; |
57 | 52 | use function reset; |
58 | 53 | use function spl_object_id; |
59 | 54 | use function sprintf; |
@@ -353,7 +348,7 @@ final protected function extractIdentifierTypes(array $id, ClassMetadata $versio |
353 | 348 | $types = []; |
354 | 349 |
|
355 | 350 | foreach ($id as $field => $value) { |
356 | | - $types = [...$types, ...$this->getTypes($field, $value, $versionedClass)]; |
| 351 | + $types = [...$types, ...PersisterHelper::inferParameterTypes($field, $value, $versionedClass, $this->em)]; |
357 | 352 | } |
358 | 353 |
|
359 | 354 | return $types; |
@@ -919,8 +914,8 @@ public function expandCriteriaParameters(Criteria $criteria): array |
919 | 914 | continue; |
920 | 915 | } |
921 | 916 |
|
922 | | - $sqlParams = [...$sqlParams, ...$this->getValues($value)]; |
923 | | - $sqlTypes = [...$sqlTypes, ...$this->getTypes($field, $value, $this->class)]; |
| 917 | + $sqlParams = [...$sqlParams, ...PersisterHelper::convertToParameterValue($value, $this->em)]; |
| 918 | + $sqlTypes = [...$sqlTypes, ...PersisterHelper::inferParameterTypes($field, $value, $this->class, $this->em)]; |
924 | 919 | } |
925 | 920 |
|
926 | 921 | return [$sqlParams, $sqlTypes]; |
@@ -1858,8 +1853,8 @@ public function expandParameters(array $criteria): array |
1858 | 1853 | continue; // skip null values. |
1859 | 1854 | } |
1860 | 1855 |
|
1861 | | - $types = [...$types, ...$this->getTypes($field, $value, $this->class)]; |
1862 | | - $params = array_merge($params, $this->getValues($value)); |
| 1856 | + $types = [...$types, ...PersisterHelper::inferParameterTypes($field, $value, $this->class, $this->em)]; |
| 1857 | + $params = [...$params, ...PersisterHelper::convertToParameterValue($value, $this->em)]; |
1863 | 1858 | } |
1864 | 1859 |
|
1865 | 1860 | return [$params, $types]; |
@@ -1887,130 +1882,13 @@ private function expandToManyParameters(array $criteria): array |
1887 | 1882 | continue; // skip null values. |
1888 | 1883 | } |
1889 | 1884 |
|
1890 | | - $types = [...$types, ...$this->getTypes($criterion['field'], $criterion['value'], $criterion['class'])]; |
1891 | | - $params = array_merge($params, $this->getValues($criterion['value'])); |
| 1885 | + $types = [...$types, ...PersisterHelper::inferParameterTypes($criterion['field'], $criterion['value'], $criterion['class'], $this->em)]; |
| 1886 | + $params = [...$params, ...PersisterHelper::convertToParameterValue($criterion['value'], $this->em)]; |
1892 | 1887 | } |
1893 | 1888 |
|
1894 | 1889 | return [$params, $types]; |
1895 | 1890 | } |
1896 | 1891 |
|
1897 | | - /** |
1898 | | - * Infers field types to be used by parameter type casting. |
1899 | | - * |
1900 | | - * @return list<ParameterType|ArrayParameterType|int|string> |
1901 | | - * @phpstan-return list<ParameterType::*|ArrayParameterType::*|string> |
1902 | | - * |
1903 | | - * @throws QueryException |
1904 | | - */ |
1905 | | - private function getTypes(string $field, mixed $value, ClassMetadata $class): array |
1906 | | - { |
1907 | | - $types = []; |
1908 | | - |
1909 | | - switch (true) { |
1910 | | - case isset($class->fieldMappings[$field]): |
1911 | | - $types = array_merge($types, [$class->fieldMappings[$field]->type]); |
1912 | | - break; |
1913 | | - |
1914 | | - case isset($class->associationMappings[$field]): |
1915 | | - $assoc = $this->em->getMetadataFactory()->getOwningSide($class->associationMappings[$field]); |
1916 | | - $class = $this->em->getClassMetadata($assoc->targetEntity); |
1917 | | - |
1918 | | - if ($assoc->isManyToManyOwningSide()) { |
1919 | | - $columns = $assoc->relationToTargetKeyColumns; |
1920 | | - } else { |
1921 | | - assert($assoc->isToOneOwningSide()); |
1922 | | - $columns = $assoc->sourceToTargetKeyColumns; |
1923 | | - } |
1924 | | - |
1925 | | - foreach ($columns as $column) { |
1926 | | - $types[] = PersisterHelper::getTypeOfColumn($column, $class, $this->em); |
1927 | | - } |
1928 | | - |
1929 | | - break; |
1930 | | - |
1931 | | - default: |
1932 | | - $types[] = ParameterType::STRING; |
1933 | | - break; |
1934 | | - } |
1935 | | - |
1936 | | - if (is_array($value)) { |
1937 | | - return array_map($this->getArrayBindingType(...), $types); |
1938 | | - } |
1939 | | - |
1940 | | - return $types; |
1941 | | - } |
1942 | | - |
1943 | | - /** @phpstan-return ArrayParameterType::* */ |
1944 | | - private function getArrayBindingType(ParameterType|int|string $type): ArrayParameterType|int |
1945 | | - { |
1946 | | - if (! $type instanceof ParameterType) { |
1947 | | - $type = Type::getType((string) $type)->getBindingType(); |
1948 | | - } |
1949 | | - |
1950 | | - return match ($type) { |
1951 | | - ParameterType::STRING => ArrayParameterType::STRING, |
1952 | | - ParameterType::INTEGER => ArrayParameterType::INTEGER, |
1953 | | - ParameterType::ASCII => ArrayParameterType::ASCII, |
1954 | | - ParameterType::BINARY => ArrayParameterType::BINARY, |
1955 | | - }; |
1956 | | - } |
1957 | | - |
1958 | | - /** |
1959 | | - * Retrieves the parameters that identifies a value. |
1960 | | - * |
1961 | | - * @return mixed[] |
1962 | | - */ |
1963 | | - private function getValues(mixed $value): array |
1964 | | - { |
1965 | | - if (is_array($value)) { |
1966 | | - $newValue = []; |
1967 | | - |
1968 | | - foreach ($value as $itemValue) { |
1969 | | - $newValue = array_merge($newValue, $this->getValues($itemValue)); |
1970 | | - } |
1971 | | - |
1972 | | - return [$newValue]; |
1973 | | - } |
1974 | | - |
1975 | | - return $this->getIndividualValue($value); |
1976 | | - } |
1977 | | - |
1978 | | - /** |
1979 | | - * Retrieves an individual parameter value. |
1980 | | - * |
1981 | | - * @phpstan-return list<mixed> |
1982 | | - */ |
1983 | | - private function getIndividualValue(mixed $value): array |
1984 | | - { |
1985 | | - if (! is_object($value)) { |
1986 | | - return [$value]; |
1987 | | - } |
1988 | | - |
1989 | | - if ($value instanceof BackedEnum) { |
1990 | | - return [$value->value]; |
1991 | | - } |
1992 | | - |
1993 | | - $valueClass = DefaultProxyClassNameResolver::getClass($value); |
1994 | | - |
1995 | | - if ($this->em->getMetadataFactory()->isTransient($valueClass)) { |
1996 | | - return [$value]; |
1997 | | - } |
1998 | | - |
1999 | | - $class = $this->em->getClassMetadata($valueClass); |
2000 | | - |
2001 | | - if ($class->isIdentifierComposite) { |
2002 | | - $newValue = []; |
2003 | | - |
2004 | | - foreach ($class->getIdentifierValues($value) as $innerValue) { |
2005 | | - $newValue = array_merge($newValue, $this->getValues($innerValue)); |
2006 | | - } |
2007 | | - |
2008 | | - return $newValue; |
2009 | | - } |
2010 | | - |
2011 | | - return [$this->em->getUnitOfWork()->getSingleIdentifierValue($value)]; |
2012 | | - } |
2013 | | - |
2014 | 1892 | public function exists(object $entity, Criteria|null $extraConditions = null): bool |
2015 | 1893 | { |
2016 | 1894 | $criteria = $this->class->getIdentifierValues($entity); |
|
0 commit comments