Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I am new to the Yii Framework.

I discovered HAS MANY and BELONGS TO in the documentation, however I am confused about these features.

Can someone give some real life examples with a diagram that will clear up my doubts?

share|improve this question

closed as off topic by Mark Storey-Smith, Paul White, Mat, Jon Seigel, Max Vernon Apr 29 '13 at 4:25

Questions on Database Administrators Stack Exchange are expected to relate to database administration within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Where did you see "HAS MANY" and "BELONGS TO"? I don't see them day to day as DB Developer/DBA/Designer... –  gbn Mar 26 '12 at 7:06
    
I have seen them in php Yii Framework. –  newuser Mar 26 '12 at 7:12
    
then it's more of a php yii terminology than a general database term. –  gbn Mar 26 '12 at 7:14
    
I have updated the question with php and Yii framework. –  newuser Mar 26 '12 at 7:17

1 Answer 1

Suppose you've a blog every blog have some users and posts. Every post belongs to a user. So in that case you can create a relation on user BELONGS TO posts in yii framework.

relation in yii can be defined as :

'VarName'=>array('RelationType', 'ClassName', 'ForeignKey')

In case of post every post belongs to a user. So relation looks like this.

    public function relations() {
        return array(
            'user'=>array(self::BELONGS_TO, 'User', 'iduser'),    
        );
    }

Similarly, a user can have multiple posts. So relation for user as:

    public function relations() {
        return array(
            'posts'=>array(self::HAS_MANY, 'Post', 'iduser'),
        );
    }

For reference you can check Working with relations in Yii Framework

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.