/** * * @param string $fieldName * @param Entity $entity * @param ValidationState $validationState */ public function __construct($fieldName, Entity $entity, Request $request, ValidationState $validationState) { $this->fieldName = $fieldName; $this->entity = $entity; $this->modelTable = $this->entity->getModelTable(); $this->request = $request; $this->fieldDefinitionHelper = $this->modelTable->getFieldDefinitionHelper(); $this->configurationHelper = new \PvikAdminTools\Library\ConfigurationHelper(); $this->configurationHelper->setCurrentTable($this->modelTable->getModelTableName()); $this->validationState = $validationState; if ($this->entity->getPrimaryKey() == null || $this->entity->getPrimaryKey() == '') { $this->isNewEntity = true; } else { $this->isNewEntity = false; } $this->preset = ''; }
/** * Deletes the reference from entities to the object by the current foreign key for a field name * @param \Pvik\Database\ORM\Entity $object * @param string $fieldName */ public function deleteForeignKeyReference(\Pvik\Database\ORM\Entity $object, $fieldName) { $helper = $this->modelTable->getFieldDefinitionHelper(); $foreignModelTable = $helper->getModelTable($fieldName); // get the key that refers to the foreign object (AuthorID from a book) $foreignKey = $object->getFieldData($fieldName); $foreignObject = $foreignModelTable->getCache()->loadByPrimaryKey($foreignKey); // if object exist in cache and needs to be updated if ($foreignObject != null) { // look through foreign model $foreignHelper = $foreignModelTable->getFieldDefinitionHelper(); foreach ($foreignHelper->getManyForeignObjectsFieldList() as $foreignModelTableFieldName) { // searching for a ManyForeignObjects field with ForeignKey reference to this field if ($foreignHelper->getModelTableName($foreignModelTableFieldName) == $this->modelTable->getModelTableName() && $foreignHelper->getForeignKeyFieldName($foreignModelTableFieldName) == $fieldName) { // Author.Books.ForeignKey is AuthorID $oldKeys = $foreignObject->getFieldData($foreignModelTableFieldName); // delete from old keys $foreignObject->setFieldData($foreignModelTableFieldName, str_replace($object->getPrimaryKey(), '', $oldKeys)); break; } } } }