Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
hasScoreSorting example
$query
->
addState
(
EntityDefinitionQueryHelper::HAS_TO_MANY_JOIN
)
;
$primary
=
$definition
->
getPrimaryKeys
(
)
->
first
(
)
;
\
assert
(
$primary
instanceof StorageAware
)
;
$select
= 'SUM(' .
implode
(
' + ',
$queries
->
getWheres
(
)
)
. ') / ' . \
sprintf
(
'COUNT(%s.%s)',
$definition
->
getEntityName
(
)
,
$primary
->
getStorageName
(
)
)
;
$query
->
addSelect
(
$select
. ' as _score'
)
;
// Sort by _score primarily if the criteria has a score query or search term
if
(
!
$this
->
hasScoreSorting
(
$criteria
)
)
{
$criteria
->
addSorting
(
new
FieldSorting
(
'_score', FieldSorting::DESCENDING
)
)
;
}
$minScore
=
array_map
(
fn
(
ScoreQuery
$query
)
=>
$query
->
getScore
(
)
,
$criteria
->
getQueries
(
)
)
;
\
assert
(
!
empty
(
$minScore
)
)
;
$minScore
=
min
(
$minScore
)
;
$query
->
andHaving
(
'_score >= :_minScore'
)
;
$query
->
setParameter
(
'_minScore',
$minScore
)
;
$query
->
addState
(
'_score'
)
;