Many To One Relationship

Hi all,

On my scenario I have the following 2 tables (models) on Yii framework:

First table:

Person (id, name)

id	name

1	Dan

2	Eva

3	Criss

4	Luis

5	Alex

6	Anna

7	Steve

8	Camy

9	WithoutChildren

10	WithoutParent

The second table is:

PersonParent (id, parent_id)

id	parent_id

3	2

3	1

4	7

4	8

5	3

5	4

6	3

6	4

Person relations:


'parents' => array(



	'PersonParents(parent_id, id)'


'children' => array(



	'PersonParents(id, parent_id)',



How can I define some functions/methods ( or relations?) on Person model to access them all over the framework, something like this:

  • hasParents(id)

  • hasChildren(id)

  • getChildren(id)

  • getParents(id)

  • getChildrenWithoutParents

  • getParentsWithoutChildren


It’s quite simple. For example:

public static function getParentsWithoutChildren()


    return self::model()->findAll(... here's your confition...);


But I’m still not sure that MANY_MANY is a good way to represent parent-child relation.

You can end up with a person who has three parents :D

Actually, I’ve never done that before, so I wonder what’s the best way to do it. Belongs_to on two fields?..

Is there another better design tables for this concept? Another idea will be great.

;D (maybe more step parents). Yes, on my scenario, (on many cases -> over 70%) a person can have more than 2 parents.

I don`t understand (can you give me an example?).

I meant mother_id and father_id, but seems like you have some kind of aliens in your app, so I give up :)

So MANY_MANY it is, ok.