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
4fd13075
Commit
4fd13075
authored
3 years ago
by
colemanw
Browse files
Options
Downloads
Patches
Plain Diff
APIv4 - Support FK field lookups in create/update/save actions
parent
182bc25d
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Civi/Api4/Generic/Traits/DAOActionTrait.php
+29
-0
29 additions, 0 deletions
Civi/Api4/Generic/Traits/DAOActionTrait.php
tests/phpunit/api/v4/Action/FkJoinTest.php
+4
-3
4 additions, 3 deletions
tests/phpunit/api/v4/Action/FkJoinTest.php
with
33 additions
and
3 deletions
Civi/Api4/Generic/Traits/DAOActionTrait.php
+
29
−
0
View file @
4fd13075
...
...
@@ -164,6 +164,35 @@ trait DAOActionTrait {
return
$result
;
}
/**
* @inheritDoc
*/
protected
function
formatWriteValues
(
&
$record
)
{
$this
->
resolveFKValues
(
$record
);
parent
::
formatWriteValues
(
$record
);
}
/**
* Looks up an id based on some other property of an fk entity
*
* @param array $record
*/
private
function
resolveFKValues
(
array
&
$record
):
void
{
foreach
(
$record
as
$key
=>
$value
)
{
if
(
substr_count
(
$key
,
'.'
)
!==
1
)
{
continue
;
}
[
$fieldName
,
$fkField
]
=
explode
(
'.'
,
$key
);
$field
=
$this
->
entityFields
()[
$fieldName
]
??
NULL
;
if
(
!
$field
||
empty
(
$field
[
'fk_entity'
]))
{
continue
;
}
$fkDao
=
CoreUtil
::
getBAOFromApiName
(
$field
[
'fk_entity'
]);
$record
[
$fieldName
]
=
\CRM_Core_DAO
::
getFieldValue
(
$fkDao
,
$value
,
'id'
,
$fkField
);
unset
(
$record
[
$key
]);
}
}
/**
* @param array $params
* @param int $entityId
...
...
This diff is collapsed.
Click to expand it.
tests/phpunit/api/v4/Action/FkJoinTest.php
+
4
−
3
View file @
4fd13075
...
...
@@ -173,17 +173,18 @@ class FkJoinTest extends UnitTestCase {
->
addValue
(
'name'
,
uniqid
(
'join3'
))
->
execute
()
->
first
()[
'name'
];
// Create using pseudoconstant syntax (:name)
$cid1
=
Contact
::
create
(
FALSE
)
->
addValue
(
'first_name'
,
'Aaa'
)
->
addChain
(
'tag1'
,
EntityTag
::
create
()
->
setValues
([
'entity_id'
=>
'$id'
,
'tag_id:name'
=>
$tag1
]))
->
addChain
(
'tag2'
,
EntityTag
::
create
()
->
setValues
([
'entity_id'
=>
'$id'
,
'tag_id:name'
=>
$tag2
]))
->
execute
()
->
first
()[
'id'
];
// Create using fk syntax (.name)
$cid2
=
Contact
::
create
(
FALSE
)
->
addValue
(
'first_name'
,
'Bbb'
)
->
addChain
(
'tag1'
,
EntityTag
::
create
()
->
setValues
([
'entity_id'
=>
'$id'
,
'tag_id
:
name'
=>
$tag1
]))
->
addChain
(
'tag3'
,
EntityTag
::
create
()
->
setValues
([
'entity_id'
=>
'$id'
,
'tag_id
:
name'
=>
$tag3
]))
->
addChain
(
'tag1'
,
EntityTag
::
create
()
->
setValues
([
'entity_id'
=>
'$id'
,
'tag_id
.
name'
=>
$tag1
]))
->
addChain
(
'tag3'
,
EntityTag
::
create
()
->
setValues
([
'entity_id'
=>
'$id'
,
'tag_id
.
name'
=>
$tag3
]))
->
execute
()
->
first
()[
'id'
];
$cid3
=
Contact
::
create
(
FALSE
)
...
...
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