Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
CWD
CwdDataDoctrineORMBundle
Commits
2e069d4e
Commit
2e069d4e
authored
Sep 11, 2018
by
Bernhard Schussek
Browse files
Merge branch 'feature/avz-event' into 'master'
ÖWM AVZ registration tweaks See merge request
!1
parents
57fd2051
871db92d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Repository/ORMRepository.php
View file @
2e069d4e
...
...
@@ -122,29 +122,35 @@ abstract class ORMRepository extends EntityRepository implements EditableReposit
*/
public
function
getMultiple
(
array
$ids
,
bool
$ignoreMissing
=
false
):
Cursor
{
if
(
0
===
count
(
$ids
)
)
{
if
(
[]
===
$ids
)
{
return
new
EmptyCursor
();
}
// Index by ID for the ID check below
$
result
s
=
$this
->
createQueryBuilder
(
'e'
,
sprintf
(
'e.%s'
,
$this
->
idField
))
$
aggregate
s
=
$this
->
createQueryBuilder
(
'e'
,
sprintf
(
'e.%s'
,
$this
->
idField
))
->
where
(
sprintf
(
'e.%s IN (:ids)'
,
$this
->
idField
))
->
setParameter
(
'ids'
,
array_map
(
'strval'
,
array_values
(
$ids
)))
->
getQuery
()
->
execute
();
$cursor
=
new
ArrayCursor
(
$aggregates
);
if
(
$ignoreMissing
)
{
return
$cursor
;
}
$notFoundIds
=
array_filter
(
$ids
,
function
(
Id
$id
)
use
(
$
result
s
)
{
return
!
isset
(
$
result
s
[
$id
->
toString
()]);
function
(
Id
$id
)
use
(
$
aggregate
s
)
{
return
!
isset
(
$
aggregate
s
[
$id
->
toString
()]);
}
);
if
(
!
$ignoreMissing
&&
count
(
$notFoundIds
)
>
0
)
{
if
(
[]
!==
$notFoundIds
)
{
throw
NoResultFound
::
objectsWithIds
(
$notFoundIds
,
$this
->
getClassName
());
}
return
new
ArrayCursor
(
array_values
(
$results
))
;
return
$cursor
;
}
/**
...
...
@@ -152,7 +158,7 @@ abstract class ORMRepository extends EntityRepository implements EditableReposit
*/
public
function
getMultipleBy
(
array
$fieldValues
):
Cursor
{
if
(
0
===
count
(
$fieldValues
)
)
{
if
(
[]
===
$fieldValues
)
{
return
new
EmptyCursor
();
}
...
...
@@ -213,30 +219,58 @@ abstract class ORMRepository extends EntityRepository implements EditableReposit
return
[];
}
// Make sure we don't pass through any invalid field names
$fieldNames
=
array_intersect
(
$fieldNames
,
$this
->
_class
->
getFieldNames
());
$fieldNames
=
$this
->
retrieveFieldsToSelect
(
$fieldNames
);
$suggestFieldName
=
$this
->
getSuggestFieldName
();
$containsEmbeddedFields
=
array_reduce
(
$fieldNames
,
function
(
$containsEmbeddedFields
,
string
$field
):
bool
{
return
$containsEmbeddedFields
||
false
!==
mb_strpos
(
$field
,
'.'
);
},
false
);
$qb
=
$this
->
createQueryBuilder
(
'e'
)
->
select
(
'e.'
.
implode
(
', e.'
,
$fieldNames
));
$orExpressions
=
array_map
(
function
(
string
$field
):
string
{
return
sprintf
(
'distance(e.%s, :text) < %f'
,
$field
,
self
::
SUGGEST_DISTANCE_THRESHOLD
);
},
$this
->
getSuggestFieldNames
()
);
$qb
=
$this
->
createQueryBuilder
(
'e'
)
->
select
(
'e.'
.
implode
(
', e.'
,
$fieldNames
))
->
where
(
sprintf
(
'distance(e.%s, :text) < %f'
,
$suggestFieldName
,
self
::
SUGGEST_DISTANCE_THRESHOLD
));
$qb
->
where
(
$qb
->
expr
()
->
orX
(
...
$orExpressions
));
$qb
->
setParameter
(
'text'
,
$text
);
foreach
(
$filters
as
$field
=>
$value
)
{
$qb
->
andWhere
(
sprintf
(
'e.%1$s = :filter%1$s'
,
$field
))
->
setParameter
(
sprintf
(
'filter%s'
,
$field
),
$value
);
$qb
->
andWhere
(
sprintf
(
'e.%1$s = :filter%1$s'
,
$field
))
->
setParameter
(
sprintf
(
'filter%s'
,
$field
),
$value
)
;
}
return
$qb
->
orderBy
(
sprintf
(
'distance(e.%s, :text)'
,
$suggestFieldName
))
->
setParameter
(
'text'
,
$text
)
foreach
(
$this
->
getSuggestFieldNames
()
as
$field
)
{
$qb
->
addOrderBy
(
sprintf
(
'distance(e.%s, :text)'
,
$field
));
}
$result
=
$qb
->
setMaxResults
(
self
::
SUGGEST_LIMIT
)
->
getQuery
()
->
setHydrationMode
(
Query
::
HYDRATE_SCALAR
)
->
execute
();
->
execute
()
;
if
(
!
$containsEmbeddedFields
)
{
return
$result
;
}
return
array_map
(
[
self
::
class
,
'convertEmbeddablePath'
],
$result
);
}
/**
...
...
@@ -284,6 +318,17 @@ abstract class ORMRepository extends EntityRepository implements EditableReposit
return
[
$this
->
getEntityName
()];
}
/**
* @param string[] $fieldNames
*
* @return string[]
*/
protected
function
retrieveFieldsToSelect
(
array
$fieldNames
):
array
{
// Make sure we don't pass through any invalid field names
return
array_intersect
(
$fieldNames
,
$this
->
_class
->
getFieldNames
());
}
/**
* Synchronizes the changes of all objects of the given class names with
* the database.
...
...
@@ -293,7 +338,7 @@ abstract class ORMRepository extends EntityRepository implements EditableReposit
*
* @throws DuplicateValue If a unique constraint was violated
*/
protected
function
doFlush
(
array
$classNames
)
protected
function
doFlush
(
array
$classNames
)
:
void
{
foreach
(
$classNames
as
$className
)
{
// Get fresh identity map after every flush() (orphan removal!)
...
...
@@ -470,11 +515,11 @@ abstract class ORMRepository extends EntityRepository implements EditableReposit
/**
* Returns the field used for suggesting.
*
* @return string The field name used for suggesting
* @return string
[]
The field name
s
used for suggesting
*/
protected
function
getSuggestFieldName
():
string
protected
function
getSuggestFieldName
s
():
array
{
return
'name'
;
return
[
'name'
]
;
}
/**
...
...
@@ -564,4 +609,23 @@ abstract class ORMRepository extends EntityRepository implements EditableReposit
return
QueryBuilderBuilder
::
create
(
$queryBuilder
,
'e'
,
$offsetStrategy
);
}
private
static
function
convertEmbeddablePath
(
array
$array
):
array
{
$converted
=
[];
foreach
(
$array
as
$path
=>
$value
)
{
(
function
(
&
$array
,
string
$path
,
$value
)
:
void
{
$keys
=
explode
(
'.'
,
$path
);
foreach
(
$keys
as
$key
)
{
$array
=
&
$array
[
$key
];
}
$array
=
$value
;
})(
$converted
,
$path
,
$value
);
}
return
$converted
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment