Ejemplo n.º 1
0
func (b *Backend) analyzeRelations(info *db.ModelInfo) apperror.Error {
	for _, field := range info.TransientFields() {
		relatedItem := reflect.New(field.StructType())
		relatedInfo, err := b.InfoForModel(relatedItem)
		if err != nil {
			// No collection found, so assume a regular attribute.
			attr := db.BuildAttribute(field)
			info.AddAttribute(attr)
			continue
		}

		relation := db.BuildRelation(field)
		relation.SetModel(info)
		relation.SetRelatedModel(relatedInfo)

		if !relation.IsMany() {
			relation.SetRelationType(db.RELATION_TYPE_HAS_ONE)
		} else {
			relation.SetRelationType(db.RELATION_TYPE_HAS_MANY)
		}
		info.AddRelation(relation)
	}

	info.SetTransientFields(nil)

	return nil
}