Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
getCollectionClass example
/** * @return EntityCollection<Entity> */
public
function
read
(
EntityDefinition
$definition
, Criteria
$criteria
, Context
$context
)
: EntityCollection
{
$criteria
->
resetSorting
(
)
;
$criteria
->
resetQueries
(
)
;
/** @var EntityCollection<Entity> $collectionClass */
$collectionClass
=
$definition
->
getCollectionClass
(
)
;
$fields
=
$this
->criteriaFieldsResolver->
resolve
(
$criteria
,
$definition
)
;
return
$this
->
_read
(
$criteria
,
$definition
,
$context
,
new
$collectionClass
(
)
,
$definition
->
getFields
(
)
->
getBasicFields
(
)
,
true,
$fields
)
;
$uses
=
[
]
;
switch
(
true
)
{
case
$field
instanceof ParentAssociationField:
$uses
[
]
=
$this
->
getUsage
(
$definition
->
getEntityClass
(
)
)
;
$type
=
$this
->
getClassTypeHint
(
$definition
->
getEntityClass
(
)
)
;
break
;
case
$field
instanceof ChildrenAssociationField:
$uses
[
]
=
$this
->
getUsage
(
$definition
->
getCollectionClass
(
)
)
;
$type
=
$this
->
getClassTypeHint
(
$definition
->
getCollectionClass
(
)
)
;
break
;
case
$field
instanceof OneToOneAssociationField:
case
$field
instanceof ManyToOneAssociationField:
$uses
[
]
=
$this
->
getUsage
(
$field
->
getReferenceDefinition
(
)
->
getEntityClass
(
)
)
;
$type
=
$this
->
getClassTypeHint
(
$field
->
getReferenceDefinition
(
)
->
getEntityClass
(
)
)
;
break
;
case
$field
instanceof OneToManyAssociationField:
$uses
[
]
=
$this
->
getUsage
(
$field
->
getReferenceDefinition
(
)
->
getCollectionClass
(
)
)
;
new
EntitySearchedEvent
(
$criteria
,
$this
->definition,
$context
)
)
;
$entities
=
$this
->
read
(
$criteria
,
$context
)
;
return
new
EntitySearchResult
(
$this
->definition->
getEntityName
(
)
,
$entities
->
count
(
)
,
$entities
,
$aggregations
,
$criteria
,
$context
)
;
}
$ids
=
$this
->
searchIds
(
$criteria
,
$context
)
;
if
(
empty
(
$ids
->
getIds
(
)
)
)
{
/** @var TEntityCollection $collection */
$collection
=
$this
->definition->
getCollectionClass
(
)
;
return
new
EntitySearchResult
(
$this
->definition->
getEntityName
(
)
,
$ids
->
getTotal
(
)
,
new
$collection
(
)
,
$aggregations
,
$criteria
,
$context
)
;
}
$readCriteria
=
$criteria
->
cloneForRead
(
$ids
->
getIds
(
)
)
;
$entities
=
$this
->
read
(
$readCriteria
,
$context
)
;
$search
=
$ids
->
getData
(
)
;
foreach
(
$entities
as
$element
)
{
private
function
hydrateEntityAggregation
(
EntityAggregation
$aggregation
, array
$result
, Context
$context
)
: EntityResult
{
if
(
\
array_key_exists
(
$aggregation
->
getName
(
)
,
$result
)
)
{
$result
=
$result
[
$aggregation
->
getName
(
)
]
;
}
$ids
=
array_column
(
$result
[
'buckets'
]
, 'key'
)
;
if
(
empty
(
$ids
)
)
{
$definition
=
$this
->registry->
getByEntityName
(
$aggregation
->
getEntity
(
)
)
;
/** @var EntityCollection<Entity> $class */
$class
=
$definition
->
getCollectionClass
(
)
;
return
new
EntityResult
(
$aggregation
->
getName
(
)
,
new
$class
(
)
)
;
}
$repository
=
$this
->registry->
getRepository
(
$aggregation
->
getEntity
(
)
)
;
$entities
=
$repository
->
search
(
new
Criteria
(
$ids
)
,
$context
)
;
return
new
EntityResult
(
$aggregation
->
getName
(
)
,
$entities
->
getEntities
(
)
)
;
}
private
function
hydrateDateHistogram
(
DateHistogramAggregation
$aggregation
, array
$result
, Context
$context
)
: ?DateHistogramResult
{
if
(
!RepositorySearchDetector::
isSearchRequired
(
$this
->definition,
$criteria
)
)
{
$entities
=
$this
->
read
(
$criteria
,
$salesChannelContext
)
;
return
new
EntitySearchResult
(
$this
->definition->
getEntityName
(
)
,
$entities
->
count
(
)
,
$entities
,
$aggregations
,
$criteria
,
$salesChannelContext
->
getContext
(
)
)
;
}
$ids
=
$this
->
doSearch
(
$criteria
,
$salesChannelContext
)
;
if
(
empty
(
$ids
->
getIds
(
)
)
)
{
/** @var TEntityCollection $collection */
$collection
=
$this
->definition->
getCollectionClass
(
)
;
return
new
EntitySearchResult
(
$this
->definition->
getEntityName
(
)
,
$ids
->
getTotal
(
)
,
new
$collection
(
)
,
$aggregations
,
$criteria
,
$salesChannelContext
->
getContext
(
)
)
;
}
$readCriteria
=
$criteria
->
cloneForRead
(
$ids
->
getIds
(
)
)
;
$entities
=
$this
->
read
(
$readCriteria
,
$salesChannelContext
)
;
$search
=
$ids
->
getData
(
)
;
foreach
(
$entities
as
$element
)
{