Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Pasteque
pasteque-server
Commits
ae166681
Commit
ae166681
authored
Mar 10, 2020
by
Karamel
Browse files
Fix fromStruct association arrays, with [] or null to clear and not set to keep.
parent
4abff17e
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/lib/System/DAO/DoctrineModel.php
View file @
ae166681
...
...
@@ -282,15 +282,19 @@ abstract class DoctrineModel
}
$fieldName
=
$field
[
'name'
];
// Check for required (not null)
if
(
empty
(
$field
[
'null'
])
&&
empty
(
$struct
[
$fieldName
]))
{
if
(
empty
(
$field
[
'null'
])
&&
(
!
array_key_exists
(
$fieldName
,
$struct
)
||
$struct
[
$fieldName
]
===
null
))
{
if
(
empty
(
$field
[
'array'
]))
{
throw
new
\
UnexpectedValueException
(
sprintf
(
'%s is required'
,
$fieldName
));
}
else
{
// Allow ommiting empty arrays as 'keep'
// Allow omitting an array for keeping its value
// or setting it to null to clear it.
}
}
// Get value and assign it.
if
(
!
empty
(
$struct
[
$fieldName
]))
{
if
(
array_key_exists
(
$fieldName
,
$struct
)
// is set
&&
$struct
[
$fieldName
]
!==
null
// and not null
&&
(
!
is_array
(
$struct
[
$fieldName
])
||
count
(
$struct
[
$fieldName
]
==
0
)))
{
// and not an empty array
$value
=
$model
->
readAssociationValue
(
$struct
,
$dao
,
$field
);
if
(
!
empty
(
$field
[
'array'
]))
{
/* At that point the model is filled from database by Doctrine.
...
...
@@ -349,7 +353,8 @@ abstract class DoctrineModel
$setter
=
$model
->
findMethodNameOrThrow
(
'set'
,
$fieldName
);
call_user_func
([
$model
,
$setter
],
$value
);
}
}
else
if
(
isset
(
$struct
[
$fieldName
])
&&
$struct
[
$fieldName
]
===
null
)
{
}
else
if
(
array_key_exists
(
$fieldName
,
$struct
))
{
// either null or an empty array
$setter
=
$model
->
findMethodNameOrThrow
(
'set'
,
$fieldName
);
// Delete association on explicit null value
if
(
empty
(
$field
[
'array'
]))
{
...
...
@@ -359,7 +364,7 @@ abstract class DoctrineModel
// Clear array for arrays
call_user_func
([
$model
,
$setter
],
[]);
}
}
//
Empty an not null
: keep the current association value
}
//
Not set
: keep the current association value
which was set by Doctrine
}
return
$model
;
}
...
...
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