How to represent parent-childern relationship with erlydb?

14 views
Skip to first unread message

Takayoshi Kyou

unread,
Jul 7, 2009, 6:23:02 AM7/7/09
to erlyweb
Hi all,

I am trying to make a forum application that every forum can have
their
child-forums. But I found it hard to represent these with erlydb.

First I tried to use a self-referencing many-to-many relation:

new (A) ->
Post_data = yaws_api:parse_post(A),
{value, {parent_forum_id, Parent_forum_id_str}, Other_fields} =
lists:keytake(parent_forum_id, 1,
%% convert every keys from string to atom
[ {list_to_atom(K), V} || {K, V} <- Post_data ]),
New_forum = forum:save(forum:new_with(Other_fields)),
Parent_forum_id = list_to_integer(Parent_forum_id_str),
%% A negative parent ID means it is an orphan.
if Parent_forum_id > 0 ->
forum:add_forum( forum:find_id(Parent_forum_id), New_forum );
true ->
ok
end,
{ewr, forum, index}.


But this isn't work because the relation between parent and child is
"two-way"
that I can't detect who is the parent and who are the childern when I
use
forum:forums to access it.

Any idea?


Thanks

Alexander Dergachev

unread,
Jul 7, 2009, 9:19:37 AM7/7/09
to erl...@googlegroups.com
Hi. If I got it right, then I don't see any problem :)

As I understood the concept of subforums, any forum can have some
number of subforums, but is it really case one subforum may be placed
in more them one forum? Do we really need many-to-many relation?
--
With Best Regards,
Alexander Dergachev

Hao Chi Kiang

unread,
Jul 7, 2009, 10:06:12 AM7/7/09
to erl...@googlegroups.com
2009/7/7 Alexander Dergachev <cy6er...@gmail.com>
Oh. Maybe I'm going the wrong way...
Yes, this is only a one-to-many relations that one forum can have many
sub-forums. Is it means I should define a "self-referencing one-to-many
relation"?

But maybe why I think of many-to-many first is that I felt unnatural to write a
self-referencing one-to-many relation.

If table1 have a one-to-many relation to table2, I would say:

-module(table1).
relations () -> {one_to_many, [table2]}.

-module(table2).
relations () -> {many_to_one, [table1]}.

But it's strange to say:
relations () -> [{one_to_many, [forum]}, {many_to_one, [forum]}].

Or maybe I should just use a extra column called parent_forum_id? If I do so,
will it going to be inconvenient to access them (because erlydb won't make
access functions automatically)?


Thanks.

Takayoshi Kyou

unread,
Jul 7, 2009, 10:17:06 AM7/7/09
to erlyweb
Oops! Sorry for the wrong name... That "Hao Chi Kiang" is me :)

That is my Chinese name and Takayoshi is Japanese.

Alexander Dergachev

unread,
Jul 7, 2009, 12:27:31 PM7/7/09
to erl...@googlegroups.com
I'm not quite sure I understood you right. Where are you going to use it?

relation()->
[{many_to_one, [forum]},{one_to_many, [forum]}].

Where do you need to use it?


May be it would be more easy to help you if you show some code
examples you're going to use in your application.

Alexander Dergachev

unread,
Jul 7, 2009, 2:22:39 PM7/7/09
to erl...@googlegroups.com
OK, I guess, now I understand what you meant. You're going to create tree structure for your forum, and you're going to create "sub-sub-forum", aren't you? And as I understood, you were trying to use the same table for the all sub forums, weren't you? I guess it's impossible, and my suggestion is to create separate tables for every "layer" of your forum tree (main forum (table1) -> sub-forum layer 1 (table2) -> sub-forum layer 2 (table2) and so on).

If somebody knows any other ways for this purpose, it would be good to see.

Thanks.

/Regards,
Alexander

Takayoshi Kyou

unread,
Jul 7, 2009, 8:47:30 PM7/7/09
to erlyweb


On Jul 7, 6:22 pm, Alexander Dergachev <cy6erbr...@gmail.com> wrote:
> OK, I guess, now I understand what you meant. You're going to create tree
> structure for your forum, and you're going to create "sub-sub-forum", aren't
> you? And as I understood, you were trying to use the same table for the all
> sub forums, weren't you? I guess it's impossible, and my suggestion is to
> create separate tables for every "layer" of your forum tree (main forum
> (table1) -> sub-forum layer 1 (table2) -> sub-forum layer 2 (table2) and so
> on).
>
> If somebody knows any other ways for this purpose, it would be good to see.
>
> Thanks.
>
> /Regards,
> Alexander
>
> On Tue, Jul 7, 2009 at 8:27 PM, Alexander Dergachev <cy6erbr...@gmail.com>wrote:
>
>
>
> > I'm not quite sure I understood you right. Where are you going to use it?
>
> > relation()->
> > [{many_to_one, [forum]},{one_to_many, [forum]}].
>
> > Where do you need to use it?
>
> > May be it would be more easy to help you if you show some code
> > examples you're going to use in your application.
>
> > On 7/7/09, Hao Chi Kiang <takayoshi.k...@gmail.com> wrote:
>
> > > 2009/7/7 Alexander Dergachev <cy6erbr...@gmail.com>
>
> > >> Hi. If I got it right, then I don't see any problem :)
>
> > >> As I understood the concept of subforums, any forum can have some
> > >> number of subforums, but is it really case one subforum may be placed
> > >> in more them one forum? Do we really need many-to-many relation?
>
Yes, that's what I mean. And sorry for my bad English.
Well, I'll try your suggestion. Thanks again.

Takayoshi Kyou

unread,
Jul 10, 2009, 8:45:24 AM7/10/09
to erlyweb
Hi,

I have found my way already.

After some reading of erlydb_base, I found that it's OK to write
relations () -> [{one_to_many, [forum]}, {many_to_one, [forum]}].

Now I can access one's parent forum by forum:forum and access childern
with
forum:forums.


Regards,
Takayoshi Kyou

Alexander Dergachev

unread,
Jul 10, 2009, 9:36:37 AM7/10/09
to erl...@googlegroups.com
Hi!

Good work! And thanks for the provided solution! It's a good way to send a solution to the mail list, when you have find it out.

Have a nice weekend!

/Regards,
Alexander
Reply all
Reply to author
Forward
0 new messages