table relationship

HI

I have the table user:

user_id

user

email

group_id

also the table group:

goup_id

group

description

when I display user info I need see this:

list user:

user goup    email


max  admin   mx@gmail.com


currently I have this:

user goup email


max    1     mx@gmail.com


how we can lookup the goup_id using the model user?

same think like this

user->parent->group_id->displayGroup()

regards

max

You need to setup a relation, in your case it would look something like



<?php


class User extends CActiveRecord


{


    public function relations()


    {


        return array(


            'group'=>array(self::BELONGS_TO, 'Group', 'groupID'),


        );


    }


}


?>


Where groupID must be setup as a foreign key in your user table definition. For a more detailed description of relations look at the page:

http://www.yiiframew…de/database.arr

After that you can print the user group in the view like this:

<?php echo CHtml::encode($user->group->group); ?>

Hope this helps  :)

working!!

when updating a form is possible get all values from group with the active group_id selected

under a select box?

regards

thanks

You can add something like this to your Group model:



public function getGroupOptions() {


    $opts = array();


    $grplist=$this->findAll();


    foreach($grplist as $grp)


        $opts[$grp->group_id] = $grp->group;


    return $opts;


}


Then in your create and update views:



<?php echo CHtml::activeLabelEx($group,'group'); ?>


<?php 


echo CHtml::activeDropDownList($user,'group_id', Group::model()->groupOptions); 


?>


where "Group::model()->groupOptions" is magic for "Group::model()->getGroupOptions()".

More experienced Yii'ers may have better ideas.