self-joined m:n relation

Posts   
 
    
HcD avatar
HcD
User
Posts: 214
Joined: 12-May-2005
# Posted on: 29-May-2005 17:02:40   

Hi,

i have an issue with a "self joined m:n relation". The situation : i have a table "Article"(PK ArId) , and a linktable "ArticleRelation" (FK ArID1, FK ArID2) , which m:n relates articles to articles. In the generated code i have two collections in my ArticleEntity called ArticleRelation and ArticleRelation, which i renamed to ArticleRelationChilds and ArticleRelationParents. I would expect also a "ArticleCollectionViaArticleRelation" and "ArticleCollectionViaArticleRelation" but i don't have those. Since i now know (wink ) that i can edit the properties and relations of and entity, i discovered the "Add new m:n custom relation" in the relations tab, but when i try to add it, i cannot create the relation (the lower selection box for selecting the m:1 relation is empty) See a pic of the dialog in

And here you can see that the m:1 relations on ArticleRelation really exist :

How can i solve this ?

HcD avatar
HcD
User
Posts: 214
Joined: 12-May-2005
# Posted on: 29-May-2005 17:39:05   

As i'm trying to experiment with it, i found out something else. In my persist code somewhere i use:


                    ArticleRelationCollection arcoll = new ArticleRelationCollection();
                    foreach (ArticleRelationEntity ar in listContractArtikels_nt.Items)
                    {
                        ArticleRelationEntity new_ar = new ArticleRelationEntity(ar.ArrelAr1,ar.ArrelAr2);
                        arcoll.Add(new_ar);
                    }
                    int i = arcoll.SaveMulti();


I have an emptyARTICLE_RELATION table, and in my GUI i added a new related article to the listbox, and then try to persist. It goes nicely into the foreach, adds a new ArticleRelationEntity to the ArticleRelationCollection, and the arcoll.SaveMulti() returns 1. But when i refresh my database table ARTICLE_RELATION, there is nothing in it cry

EDIT:

i replaced the code in the foreach with


                        ArticleRelationEntity new_ar = new ArticleRelationEntity();
                        new_ar.ArrelAr1 =  ar.ArrelAr1;
                        new_ar.ArrelAr2 = ar.ArrelAr2;
                        arcoll.Add(new_ar);


and now it works. Is this normal ? Why did the collection.SaveMulti reported 1 save with the other code but didn't save anything ?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 30-May-2005 09:53:17   

m:n relations with self are not supported. This is because you can filter these out yourself using one simple join and it would probably result in a lot more m:n relations in the project.

Using the constructor to pass in PK values, will fetch the entity from the db.

Frans Bouma | Lead developer LLBLGen Pro
HcD avatar
HcD
User
Posts: 214
Joined: 12-May-2005
# Posted on: 30-May-2005 10:33:37   

Otis wrote:

Using the constructor to pass in PK values, will fetch the entity from the db.

Yes, but what if the entity doesn't exist yet in the DB. I was under the assumption that it would create a new one, and set the PK's accordingly. And that maybe using "IsNew" i could check if it is allready an existing entity.

But if i create a "new" one this way, the Save() doesn't do anything. Shouldn't it at least throw an exception then, or am i missing someting here ? I even tried setting "IsDirty = true" before calling the Save()

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 30-May-2005 11:06:35   

HcD wrote:

Otis wrote:

Using the constructor to pass in PK values, will fetch the entity from the db.

Yes, but what if the entity doesn't exist yet in the DB. I was under the assumption that it would create a new one, and set the PK's accordingly. And that maybe using "IsNew" i could check if it is allready an existing entity.

But if i create a "new" one this way, the Save() doesn't do anything. Shouldn't it at least throw an exception then, or am i missing someting here ? I even tried setting "IsDirty = true" before calling the Save()

It sets the PK fields, but it doesn't make them 'changed' for obvious reasons. (so you have a clean entity, no changed fields, which will make sure it won't get saved accidently).

Frans Bouma | Lead developer LLBLGen Pro