/** * Magic method that allows us to use the field data as properties. * Converts foreign keys to objects. * @param string $fieldName * @return mixed * @throws \Exception */ public function __get($fieldName) { $modelTable = $this->getModelTable(); $helper = $modelTable->getFieldDefinitionHelper(); if ($helper->fieldExists($fieldName)) { switch ($helper->getFieldType($fieldName)) { case FieldDefinition\Type::PRIMARY_KEY: case FieldDefinition\Type::NORMAL: case FieldDefinition\Type::FOREIGN_KEY: return $this->getFieldData($fieldName); break; case FieldDefinition\Type::FOREIGN_OBJECT: // search for the foreign key reference $foreignKeyFieldName = $helper->getForeignKeyFieldName($fieldName); $foreignKey = $this->getFieldData($foreignKeyFieldName); if ($foreignKey == null) { return null; } $foreignModelTable = $helper->getModelTable($foreignKeyFieldName); return $foreignModelTable->loadByPrimaryKey($foreignKey); break; case FieldDefinition\Type::MANY_FOREIGN_OBJECTS: $foreignKeys = $this->getFieldData($fieldName); $modelTable = $helper->getModelTable($fieldName); if ($foreignKeys == null) { $entityArray = new EntityArray(); $entityArray->setModelTable($modelTable); return $entityArray; } if (isset($foreignKeys)) { return $modelTable->loadByPrimaryKeys(explode(',', $foreignKeys)); } else { throw new \Exception('Foreign keys for ' . $fieldName . ' not found.'); } break; } } else { if ($this->fieldDataExists($fieldName)) { return $this->getFieldData($fieldName); } else { throw new \Exception('Value ' . $fieldName . ' not found.'); } } }
/** * Merge a EntityArray to current EntityArray * @param EntityArray $entityArray * @return EntityArray */ public function merge(EntityArray $entityArray) { $list = new EntityArray(); // set model table if both arrays have the same model table if ($this->getModelTable() == $entityArray->getModelTable()) { $list->setModelTable($this->getModelTable()); } foreach ($this as $object) { $list->append($object); } foreach ($entityArray as $object) { $list->append($object); } return $list; }