@param object $entity
@param string $field
@param string $adder

@return RestException
557165
public static function missingAdder($entity, $field, $adder)
    {
        $source = [
            'entity' => ClassUtils::getClass($entity),
            'adder' => $adder
        ];

        if ($entity instanceof JsonApiResource) {
            $source['pointer'] = $entity->getResourceKey().'.'.$field;
        }

        return static::createUnprocessable('Missing remover method.')
            ->error('missing-adder', $source, 'Missing collection adder.');
    }
992083
protected function generateRequiredFieldException(Entity $entity, FieldDescriptorProto $field)
    {
        $name       = $field->getName();
        $label      = $field->getLabel();
        $tag        = $field->getNumber();
        $isRequired = $label === Label::LABEL_REQUIRED();

        if ( ! $isRequired) {
            return [];
        }

        $class   = $entity->getNamespacedName();
        $format  = 'Field "%s#%s" (tag %s) is required but has no value.';
        $message = var_export(sprintf($format, $class, $name, $tag), true);

        $body[] = 'if ($this->' . $name . ' === null) {';
        $body[] = '    throw new \UnexpectedValueException(' . $message . ');';
        $body[] = '}';
        $body[] = null;

        return $body;
    }
350920
protected function guessPluralField($entity, $field = null)
    {
        return $field ?: Inflector::pluralize($this->guessSingularField($entity));
    }
601322
public function setFieldValue($entity, $fieldName, $value)
    {
        if ($this->fieldMappings[$fieldName]['direct']) {
            $this->reflFields[$fieldName]->setValue($entity, $value);
        } else {
            if (!array_key_exists('setMethod', $this->fieldMappings[$fieldName])) {
                $this->fieldMappings[$fieldName]['setMethod'] = $this->inferSetter($fieldName);
            }
            $setter = $this->fieldMappings[$fieldName]['setMethod'];

            if ($this->reflClass->hasMethod($setter)) {
                return call_user_func(array($entity, $setter), $value);
            } else {
                throw MappingException::fieldSetMethodDoesNotExist($this->name, $fieldName, $setter);
            }
        }
    }
490079
protected function generateReadCallback(Entity $entity, FieldDescriptorProto $field)
    {
        $body  = [];
        $sttm  = $this->generateFieldReadStatement($entity, $field);
        $lines = $this->addIndentation($sttm, 1);

        $body[] = '$readCallback = function (\Protobuf\ReadContext $context, $wire) {';
        $body[] = '    $reader = $context->getReader();';
        $body[] = '    $length = $context->getLength();';
        $body[] = '    $stream = $context->getStream();';
        $body[] = null;
        $body   = array_merge($body, $lines);
        $body[] = '};';

        return $body;
    }
711032
protected function generateFieldCondition(Entity $entity, FieldDescriptorProto $field)
    {
        $body      = [];
        $fieldName = $field->getName();
        $format    = 'if ($this->%s !== null) {';
        $sttm      = $this->generateFieldWriteStatement($entity, $field);
        $lines     = $this->addIndentation($sttm, 1);

        $body[] = sprintf($format, $fieldName);
        $body   = array_merge($body, $lines);
        $body[] = '}';

        return $body;
    }
703146
public function addField($name, $decorator = null, $field = null)
    {
        if (!is_string($name)) {
            throw new Exception(['Format for addField now require first argument to be name']);
        }

        if (!$this->form->model) {
            $this->form->model = new \atk4\ui\misc\ProxyModel();
        }

        if (is_string($field)) {
            $field = ['type' => $field];
        }

        if ($name) {
            $existingField = $this->form->model->hasElement($name);
        }

        try {
            if (!$existingField) {
                // Add missing field
                if ($field) {
                    $field = $this->form->model->addField($name, $field);
                } else {
                    $field = $this->form->model->addField($name);
                }
            } elseif (is_array($field)) {
                // Add properties to existing field
                $existingField->setDefaults($field);
                $field = $existingField;
            } elseif (is_object($field)) {
                throw new Exception(['Duplicate field', 'name' => $name]);
            } else {
                $field = $existingField;
            }

            if (is_string($decorator)) {
                $decorator = $this->form->decoratorFactory($field, ['caption' => $decorator]);
            } elseif (is_array($decorator)) {
                $decorator = $this->form->decoratorFactory($field, $decorator);
            } elseif (!$decorator) {
                $decorator = $this->form->decoratorFactory($field);
            } elseif (is_object($decorator)) {
                if (!$decorator instanceof \atk4\ui\FormField\Generic) {
                    throw new Exception(['Field decorator must descend from \atk4\ui\FormField\Generic', 'decorator' => $decorator]);
                }
                $decorator->field = $field;
                $decorator->form = $this->form;
            } else {
                throw new Exception(['Value of $decorator argument is incorrect', 'decorator' => $decorator]);
            }
        } catch (\Throwable $e) {
            throw new Exception(['Unable to add form field', 'name' => $name, 'decorator' => $decorator, 'field' => $field], null, $e);
        }

        return $this->_addField($decorator, $field);
    }
861127
protected function generateRequiredFieldException(Entity $entity, FieldDescriptorProto $field)
    {
        $name = $field->getName();
        $tag  = $field->getNumber();

        $class   = $entity->getNamespacedName();
        $format  = 'Field "%s" (tag %s) is required but has no value.';
        $message = var_export(sprintf($format, $name, $tag), true);

        $body[] = 'if ( ! isset($values[\'' . $name . '\'])) {';
        $body[] = '    throw new \InvalidArgumentException(' . $message . ');';
        $body[] = '}';

        return $body;
    }
1267630
private function showParentsAndChildren(\stdClass $output, ApiInterface $entity, Field $field) {
        if(in_array($field->getAlias(), $entity->getParentRelationships())) {

            /** @var ApiInterface $parentEntity */
            $parentEntity = $entity->getRelated($field->getAlias());
            if($parentEntity === false) {//AKA, a nullable vendor
                $output->{$field->getAlias()} = null;
            } else {
                $parentEntities = $entity->getApiParentEntities() ?? [];
                $parentEntities[$entity->getSource()] = $entity;
                $parentEntity->setApiParentEntities($parentEntities);
                $output->{$field->getAlias()} = new \stdClass();
                $this->showRecursive($output->{$field->getAlias()}, $field, $parentEntity);
            }
        } elseif(in_array($field->getAlias(), $entity->getChildrenRelationships())) {
            $output->{$field->getAlias()} = array();
            $resultSet = $entity->getRelated($field->getAlias());
            foreach($resultSet as $key => $currentEntity) {
                $parentEntities = $entity->getApiParentEntities() ?? [];
                $parentEntities[$entity->getSource()] = $entity;
                $currentEntity->setApiParentEntities($parentEntities);

                $subOutput = new \stdClass();
                $output->{$field->getAlias()}[] = $subOutput;
                $this->showRecursive($subOutput, $field, $currentEntity);
            }
        } else {
            throw new Exception('Invalid Field, not on the current object, no child or parent found: ' . $entity->getEntityName() . ' with ' . $field->getAlias(), 400);
        }
    }
1221308
protected function fromArrayRelationEntity($v, $p, $pn, $getter, $setter,
                                                $association, $policy,
                                                Reader $ar,
                                                PolicyResolver $pr,
                                                EntityManagerInterface $em,
                                                \ReflectionClass $refClass,
                                                $idField, $class, $subPolicy) {
        $jc = $ar->getPropertyAnnotation($p, 'Doctrine\ORM\Mapping\JoinColumn'); // find target id field name
        if($jc) { $idField = $jc->referencedColumnName; }
        $idGetter = 'get'.ucfirst($idField);  
                        
        $subEntity = null;
        
         // non-existent
        if(empty($v[$idField])) {
            if($policy instanceof Policy\Interfaces\DenyNewFrom) { return; }
            $subEntity = new $class();
            $subEntity->fromArray($v, $em, $subPolicy, $ar, $pr);
            $this->$setter($subEntity);
            
        // existent
        } else {
            $current = $this->$getter();
            
            // existent internal
            if($current && ($current->$idGetter() === $v[$idField])) { 
                $subEntity = $current;
                
            // RETRIEVING existent external
            } else if(!($policy instanceof Policy\Interfaces\DenyUpdateFrom) || $policy->allowExternal) { 
                $subEntity = $em->getReference($class, $v[$idField]);
            }
            
            // UPDATING internal or external
            if($subEntity) {
                if(!($policy instanceof Policy\Interfaces\DenyUpdateFrom) || $policy->allowExistent) {
                    $subEntity->fromArray($v, $em, $subPolicy, $ar, $pr);
                }
                $this->$setter($subEntity);
            }
            
        }
    }
1077908
protected function setKey(Field & $field, $key, $extra)
    {
        $key = strtoupper($key);

        if ($key == 'PRIMARY KEY') {
            $field->isPrimary = true;
        }

        if ($key == 'MUL') {
            $field->isIndex = true;
        }

        if ($key == 'UNI') {
            $field->isUnique = true;
        }

        if (strtolower($extra) == 'auto_increment') {
            $field->isAutoIncrement = true;
        }

        return $this;
    }
50934
public function createEntity($entity, TableNode $fields) {
    $this->models->get($entity)->store($fields->getRowsHash());
  }
1032331
private function buildFieldsMap($typeEntity = '')
	{
		if (strlen($typeEntity) == 0){
			$typeEntity = $this->entityType;
		}
		if (strlen($typeEntity) == 0){
			throw new AdminExceptions\ConfigurationModule('The entity type is not defined');
		}

		switch ($typeEntity) {
			case 'list':
			case 'edit':
				$fieldsMap = $this->configEntity[$typeEntity]['fields'];
				if (count($fieldsMap) > 0){
					foreach ($fieldsMap as $code => $value) {
						$title = $value;
						if(is_array($value)){
							$title = $value['property'];
						}

						if ($this->entity->hasField($title)){

							$field = $this->entity->getField($title);

							$dataField = $this->getScalarField($field);

							if(is_array($value)){
								$dataField += $value;
							}

							$this->fields['core'][$title] = $dataField;
						} else {

							if(!is_array($value)){
								$value = ['property' => $title];
							}
							$value['virtual'] = true;
							$this->fields['core'][$title] = array_merge(
								$this->defaultField, $value
							);
						}

						$this->fields['map'][] = $title;
					}
				} else {
					/** @var Main\Entity\Field $field */
					foreach ($this->entity->getFields() as $field) {
						$this->fields['core'][$field->getName()] = $this->getScalarField($field);
						$this->fields['map'][] = $field->getName();
					}
				}
				break;
			case 'simple':

				break;
		}

		return $this;
	}
499841
protected function getEntityAttributeValue($entity, $field)
    {
        $functionName = 'get'.ucfirst($field);

        $fieldValue = call_user_func([$entity, $functionName]);

        return $fieldValue;
    }
1209084
public function setFieldValue($object, $field, $value)
    {
        $this->assertObjectInstance($object);

        return $this->getField($field)->set($object, $value);
    }
182495
public function addField(Field $field, $key = null)
    {
        $this->fields[$key] = $field;

        return $this;
    }
960801
protected function guessSingularField($entity, $field = null)
    {
        return $field ?: Inflector::singularize(
            lcfirst(basename(str_replace('\\', '/', $entity)))
        );
    }