Yii Framework Forum: Loading Data From Another Table In Yii - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Loading Data From Another Table In Yii Rate Topic: -----

#1 User is offline   Saqib 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 14-November 12

Posted 15 November 2012 - 12:38 AM

I am totally new in Yii framework and quite frankly do not know everything about it. I have an ongoing project in which I am stuck in a problem. I have 3 tables in the project. Here are the table names and the rows:

Users (id, name, email, username, password, currentTIme) Uses for storing user information.

Groups (id, name, currentTIme) Uses for storing Group Information.

UserGroup (id, userId, groupId, currentTime) Uses for keeping the relation between user and group.

Now, what I have to do is after creating 3 models, Users and Groups model will be used for all users. Different groups was added like:

01. CSE 02. EEE 03. BBA 04. Architecture etc. etc.

Now, when 'User create' portion 2 things have to happen. One, the group names (CSE, EEE, BBA, Architecture etc.) will be shown in a listbox to choose along with the user's other information (name, password etc.). Now question is How I can show the name of Groups in a listbox in the model Users where there is no relationship.

Second problem is, when I will click 'submit' it will update 2 tables 'Users' and 'UserGroup'. Name, password,currentTime will be saved in 'User' and userid(of current user), groupid(id of the groups user will be selected like 1 for CSE etc.) will be saved in 'UserGroup' table.

Thank you for tolerating my horrible English and any kind of comment/feedback will be a big help for me.....Thanks in advance
0

#2 User is offline   Saqib 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 14-November 12

Posted 15 November 2012 - 01:16 AM

I found out answer of the first part which shows the list from another model. For anybody like me, it can be useful i am posting it here.

in the /models/users.php:

class Users extends CActiveRecord { public $Groups; ....................... ....................... }

in the /view/users/_form.php:

<div class="row">
<?php echo $form->labelEx($model,'Group'); ?>
<?php
echo $form->dropDownList($model, 'Groups',
CHtml::listData(Groups::model()->findAll(),'id','name'));

//echo $form->;dropDownList($model,'Groups', CHtml::listData(Groups::model()->findAll(), 'id', 'value'), array('empty'=>;'--please select--'));
?>
<?php echo $form->error($model,'password'); ?>
</div>
0

#3 User is offline   Ujjwal Prajapati 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 47
  • Joined: 24-April 12

Posted 16 November 2012 - 04:00 AM

Hello there,

The thing is, since the table should have some relationship, I see the table UserGroup is related to both users and groups; while creating the model, it will define relationship in the class itself. If not, then please define the relationship between tables. Please see the documentation for the relationship. Once, you have relationship defined, everything else should be pretty simple.

Thanks.
0

#4 User is offline   Saqib 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 14-November 12

Posted 18 November 2012 - 04:46 AM

View PostUjjwal Prajapati, on 16 November 2012 - 04:00 AM, said:

Hello there,

The thing is, since the table should have some relationship, I see the table UserGroup is related to both users and groups; while creating the model, it will define relationship in the class itself. If not, then please define the relationship between tables. Please see the documentation for the relationship. Once, you have relationship defined, everything else should be pretty simple.

Thanks.


At first, Thanks for your kind reply. Here, I have relationship between user and usergroup and group and usergroup. But I do not know how to set the relationship in the models(I tried, it did not work). and what else besides relationship I have to do.
again thanks for giving this novice your feedback
0

#5 User is offline   Saqib 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 14-November 12

Posted 27 November 2012 - 02:26 AM

If anybody needs help, thats why I am posting the answer. Solved it while ago but because I was busy, I could not give the answer. Sorry for that.
ANSWER:
all you have to do, need to create a variable model2 and load the posted value in your expected model through that variable (model2). Sorry for bad English 8)
UserController.php
public function actionCreate()
{
$model=new Users;

// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);

if(isset($_POST['Users']))
{

$model->attributes=$_POST['Users'];

if($model->save())
{
//do not need for you, i needed to do it for upload purpose
// $userid = $model->primaryKey;

foreach ($_POST['Users']['groupId'] as $k=>$v)
{

$model2 = new Usergroup;
$model2->userId = $userid ;
$model2->groupId = $v;
$model2->save();
}
//do not need for you, i needed to do it for upload purpose
// mkdir ("./Upload/$userid", 0700);

$this->redirect(array('view','id'=>$model->id));
}
}

$this->render('create',array(
'model'=>$model,
));
}
0

#6 User is offline   hameedhamdani 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 82
  • Joined: 20-October 12
  • Location:Islambad,Pakistan

Posted 27 November 2012 - 05:08 AM

View PostSaqib, on 15 November 2012 - 12:38 AM, said:

I am totally new in Yii framework and quite frankly do not know everything about it. I have an ongoing project in which I am stuck in a problem. I have 3 tables in the project. Here are the table names and the rows:

Users (id, name, email, username, password, currentTIme) Uses for storing user information.

Groups (id, name, currentTIme) Uses for storing Group Information.

UserGroup (id, userId, groupId, currentTime) Uses for keeping the relation between user and group.

Now, what I have to do is after creating 3 models, Users and Groups model will be used for all users. Different groups was added like:

01. CSE 02. EEE 03. BBA 04. Architecture etc. etc.

Now, when 'User create' portion 2 things have to happen. One, the group names (CSE, EEE, BBA, Architecture etc.) will be shown in a listbox to choose along with the user's other information (name, password etc.). Now question is How I can show the name of Groups in a listbox in the model Users where there is no relationship.

Second problem is, when I will click 'submit' it will update 2 tables 'Users' and 'UserGroup'. Name, password,currentTime will be saved in 'User' and userid(of current user), groupid(id of the groups user will be selected like 1 for CSE etc.) will be saved in 'UserGroup' table.

Thank you for tolerating my horrible English and any kind of comment/feedback will be a big help for me.....Thanks in advance


@Saqib,
First you create relations on database level. then run the gii for CRUD. it will automatically create your relations among models(tables).
so by using relations u can easily manager your data and dropdown and everything u want to related relational active record.
First you create CRUD then let me know what needed you???
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users