/// <summary> /// Links the current Bean with another Bean in a m:n relational manner and /// provides data (linkProps) for the Link. /// </summary> /// <param name="bean">Bean to be linked.</param> /// <param name="linkProps">Dictionary of Link Properties.</param> /// <returns>true, if successful</returns> public bool LinkWith(Bean bean, IDictionary <string, object> linkProps = null) { var ls = GetLinkScenario(bean.GetKind()); var linkedKindPkValue = bean.GetKeyValue(); var linkBean = Api.FindOne(false, ls.LinkKind, "WHERE " + ls.LinkingKindFkName + " = {0} AND " + ls.LinkedKindFkName + " = {1}", ls.LinkingKindPkValue, linkedKindPkValue); if (linkBean != null) { throw LinkAlreadyExistsException.New(ls.LinkingKind, ls.LinkedKind); } linkBean = Api.Dispense(ls.LinkKind); if (linkProps != null) { linkBean.Import(linkProps); } linkBean .Put(ls.LinkingKindFkName, ls.LinkingKindPkValue) .Put(ls.LinkedKindFkName, linkedKindPkValue) .Store(); return(true); }
/// <summary> /// Unlinks a Bean from the current Bean when they are related in a m:n relational way. /// </summary> /// <param name="bean">Bean to be unlinked.</param> /// <returns>true, if successful</returns> public bool Unlink(Bean bean) { var ls = GetLinkScenario(bean.GetKind()); var linkBean = Api.FindOne(false, ls.LinkKind, "WHERE " + ls.LinkingKindFkName + " = {0} AND " + ls.LinkedKindFkName + " = {1}", ls.LinkingKindPkValue, bean.GetKeyValue()); linkBean?.Trash(); return(true); }
/// <summary> /// Attaches an owner Bean in a Manner of a 1:n relation. The owner /// represents the "1"-side of this relation. If the Foreign Key Name /// of the (owned) Bean differs form the Name of the Owner Bean, it /// is possible to pass an alias name. /// </summary> /// <param name="bean">Owner Bean</param> /// <param name="fkAlias">Alias for the Owner Bean's Foreign Key Name (w/o Primary Key Name)</param> /// <returns>true, if successful</returns> public bool AttachOwner(Bean bean, string fkAlias = "") { var foreignKey = GetFkName(fkAlias == string.Empty ? bean.GetKind() : fkAlias); if (!Api.IsKnownKindColumn(GetKind(), foreignKey)) { throw MissingForeignKeyColumnException.Create(GetKind(), foreignKey); } Put(foreignKey, bean.GetKeyValue()); Store(); return(true); }