Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
CiviCRM Core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container registry
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Development
CiviCRM Core
Commits
ed1c8fdd
Unverified
Commit
ed1c8fdd
authored
5 years ago
by
Eileen McNaughton
Committed by
GitHub
5 years ago
Browse files
Options
Downloads
Plain Diff
Merge pull request #16274 from colemanw/type
Api4 - Convert field values to correct data type
parents
0b28857c
aec2fb31
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Civi/Api4/Event/Subscriber/PostSelectQuerySubscriber.php
+39
-11
39 additions, 11 deletions
Civi/Api4/Event/Subscriber/PostSelectQuerySubscriber.php
with
39 additions
and
11 deletions
Civi/Api4/Event/Subscriber/PostSelectQuerySubscriber.php
+
39
−
11
View file @
ed1c8fdd
...
...
@@ -60,8 +60,7 @@ class PostSelectQuerySubscriber implements EventSubscriberInterface {
return
$results
;
}
$fieldSpec
=
$query
->
getApiFieldSpec
();
$this
->
unserializeFields
(
$results
,
$query
->
getEntity
(),
$fieldSpec
);
$this
->
formatFieldValues
(
$results
,
$query
->
getApiFieldSpec
());
// Group the selects to avoid queries for each field
$groupedSelects
=
$this
->
getNtoManyJoinSelects
(
$query
);
...
...
@@ -75,7 +74,7 @@ class PostSelectQuerySubscriber implements EventSubscriberInterface {
foreach
(
$results
as
&
$primaryResult
)
{
$baseId
=
$primaryResult
[
'id'
];
$filtered
=
array_filter
(
$joinResults
,
function
(
$res
)
use
(
$baseId
)
{
return
(
$res
[
'_base_id'
]
==
=
$baseId
);
return
(
$res
[
'_base_id'
]
==
$baseId
);
});
$filtered
=
array_values
(
$filtered
);
ArrayInsertionUtil
::
insert
(
$primaryResult
,
$joinPath
,
$filtered
);
...
...
@@ -98,26 +97,55 @@ class PostSelectQuerySubscriber implements EventSubscriberInterface {
$fields
[
array_pop
(
$name
)]
=
$field
->
toArray
();
}
if
(
$fields
)
{
$this
->
unserializeField
s
(
$joinResults
,
NULL
,
$fields
);
$this
->
formatFieldValue
s
(
$joinResults
,
$fields
);
}
}
/**
* Unserialize values
* Unserialize values
and convert to correct type
*
* @param array $results
* @param string $entity
* @param array $fields
*/
protected
function
unserializeField
s
(
&
$results
,
$entity
,
$fields
=
[])
{
protected
function
formatFieldValue
s
(
&
$results
,
$fields
=
[])
{
foreach
(
$results
as
&
$result
)
{
foreach
(
$result
as
$field
=>
&
$value
)
{
if
(
!
empty
(
$fields
[
$field
][
'serialize'
])
&&
is_string
(
$value
))
{
$serializationType
=
$fields
[
$field
][
'serialize'
];
$value
=
\CRM_Core_DAO
::
unSerializeField
(
$value
,
$serializationType
);
foreach
(
$result
as
$field
=>
$value
)
{
$dataType
=
$fields
[
$field
][
'data_type'
]
??
NULL
;
if
(
!
empty
(
$fields
[
$field
][
'serialize'
]))
{
if
(
is_string
(
$value
))
{
$result
[
$field
]
=
$value
=
\CRM_Core_DAO
::
unSerializeField
(
$value
,
$fields
[
$field
][
'serialize'
]);
foreach
(
$value
as
$key
=>
$val
)
{
$result
[
$field
][
$key
]
=
$this
->
convertDataType
(
$val
,
$dataType
);
}
}
}
else
{
$result
[
$field
]
=
$this
->
convertDataType
(
$value
,
$dataType
);
}
}
}
}
/**
* @param mixed $value
* @param string $dataType
* @return mixed
*/
protected
function
convertDataType
(
$value
,
$dataType
)
{
if
(
isset
(
$value
))
{
switch
(
$dataType
)
{
case
'Boolean'
:
return
(
bool
)
$value
;
case
'Integer'
:
return
(
int
)
$value
;
case
'Money'
:
case
'Float'
:
return
(
float
)
$value
;
}
}
return
$value
;
}
/**
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment