MVC: minor cleanups and prevent virtual nodes to be persisted to disk.

o BaseField: add hasChild() and getChild() so callers don't have to use getChildren() to check if a single item exists.
o BaseField: prevent addToXMLNode() from saving virtual nodes
o BaseModel: refactor getNodeByReference() to use hasChild() and getChild()
This commit is contained in:
Ad Schellevis 2020-06-28 09:15:36 +02:00
parent f6a7df64fe
commit a0227f3fca
2 changed files with 28 additions and 8 deletions

View File

@ -582,8 +582,8 @@ abstract class BaseModel
$node = $this->internalData;
while (count($parts) > 0) {
$childName = array_shift($parts);
if (isset($node->getChildren()[$childName])) {
$node = $node->getChildren()[$childName];
if ($node->hasChild($childName)) {
$node = $node->getChild($childName);
} else {
return null;
}

View File

@ -366,11 +366,7 @@ abstract class BaseField
*/
public function isFieldChanged()
{
if ($this->internalInitialValue !== $this->internalValue) {
return true;
} else {
return false;
}
return $this->internalInitialValue !== $this->internalValue;
}
/**
@ -415,6 +411,26 @@ abstract class BaseField
return $this->internalChildnodes;
}
/**
* check for existance of child attribute
* @return bool if child exists
*/
public function hasChild($name)
{
return isset($this->internalChildnodes[$name]);
}
/**
* retrieve child object
* @return null|object
*/
public function getChild($name)
{
if ($this->hasChild($name)) {
return $this->internalChildnodes[$name];
}
}
/**
* check if this field is unused and required
* @return bool
@ -506,7 +522,7 @@ abstract class BaseField
}
/**
* returns if this node is virtual, the framework uses this to determine if this node maybe should only be used to
* returns if this node is virtual, the framework uses this to determine if this node should only be used to
* clone children. (using ArrayFields)
* @return bool is virtual node
*/
@ -632,6 +648,10 @@ abstract class BaseField
}
foreach ($this->iterateItems() as $key => $FieldNode) {
if ($FieldNode->getInternalIsVirtual()) {
// Virtual fields should never be persisted
continue;
}
$FieldNode->addToXMLNode($subnode);
}
}