Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7406057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:29:51+00:00 2026-05-29T05:29:51+00:00

In Yii I am doing multimodel.My database is something like this +++++ Group ++++++

  • 0

In Yii I am doing multimodel.My database is something like this

 +++++ Group ++++++
 id
 name

 +++++ Member ++++++
 id
 group_id
 firstname
 lastname
 membersince

In Group controller I want to show Member’s attributes.Everything is working fine but when I am using manage option from the menu it is showing the attributes for both models but in two different grid-view.I want to show both models attributes in a single grid-view.
The code for Member controller is like this

  public function actionAdmin()
  {
    $model=new Group('search');
    $model->unsetAttributes();  // clear any default values
    if(isset($_GET['Group']))
    {
      $model->attributes=$_GET['Group'];
    }
    $member=new Member('search');
    $member->unsetAttributes();  // clear any default values
    if(isset($_GET['Member']))
    {
      $model->attributes=$_GET['Member'];
    }
    $this->render('admin',array(
      'model'=>$model,
      'member'=>$member,
    ));
  }

for View in Group admin code is like this

 <?php $this->widget('zii.widgets.grid.CGridView', array(
  'id'=>'member-grid',
  'dataProvider'=>$model->search(),
  'filter'=>$model,
  'columns'=>array(
    'id',
    'name',
    array(
      'class'=>'CButtonColumn',
    ),
  ),
));
    $this->widget('zii.widgets.grid.CGridView', array(
                  'id'=>'member-grid',
                  'dataProvider'=>$member->search(),
                  'filter'=>$member,
                  'columns'=>array(
                    'firstname',
                    'lastname',
                    array(
                      'class'=>'CButtonColumn',
                    ),                    
                            ),
                 ));

Here I have used CGridView for two times to show models for both attributes. So can someone tell me how to show models in a single
CGridView.Any help and suggestions will be highly appriciable.
[Updated]
Relations in Models:
Group Model

public function relations()
  {
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
      'member' => array(self::HAS_MANY, 'Member', 'group_id'),
    );
  }

Member Model:

 public function relations()
  {
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
      'group' => array(self::BELONGS_TO, 'Group', 'group_id'),
    );
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:29:52+00:00Added an answer on May 29, 2026 at 5:29 am

    A simple way to access related model fields in yii is to use something like this
    $model->relatedModel->field — this can be used if there is a has_one, or belongs_to relation between the models.
    So in your case, you can access the group name of a member using the code
    $memberModel->group->name

    But when you need to access related model fields for has_many, or many_many relation types, you will need to do something like
    $model->relatedModel[arrayIndex]->field
    This is because there are many related models in this case, and yii automatically gives you the related model in an array.
    In your case a group has many members and to access a particular member(say the first member, i.e arrayIndex = 0) of a group you can use $groupModel->members[0]->firstname
    Now to coming to your exact question, first of all, you do not need to declare or initialize or pass the $member model. So your controller action can be

    public function actionAdmin(){
      $model=new Group('search');
      $model->unsetAttributes();  // clear any default values
      if(isset($_GET['Group'])){
         $model->attributes=$_GET['Group'];
      }
      $this->render('admin',array(
         'model'=>$model
         )
      );
    }
    

    Then obviously in your view you don’t need the two grid-views

    <?php 
       $this->widget('zii.widgets.grid.CGridView', array(
         'id'=>'member-grid',
         'dataProvider'=>$model->search(),
         'filter'=>$model,
         'columns'=>array(
             'id',
             'name',
             array( // this is for your related group members of the current group
                'name'=>'members.firstname', // this will access the attributeLabel from the member model class, and assign it to your column header
                'value'=>'$data->members[0]->firstname', // this will access the current group's 1st member and give out the firstname of that member
                'type'=>'raw' // this tells that the value type is raw and no formatting is to be applied to it
             ),
             array(
               'class'=>'CButtonColumn',
             ),
          ),
       ));
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Yii by default maps routes to: module/controller/action (like Zend) How do I disable this
Hello I'm doing file upload with Yii. I have implemented this way and it
I'm doing the Agile Yii book. Anyway, I'm trying to execute this command: INSERT
Suppose in Yii Framework I have the attributes for the model like this field_1
I am semi-frustrated with this Yii CGridView problem and any help or guidance would
In Yii framework I used migration just like ./yiic migrate create tbl_demo it made
I'm facing an issue with Yii Framework routing. I've created controller, let's call it
I am running a PHP/Yii application on Apache. I have tried doing the following:
I'm using PHP (Yii-framework) and MySQL database on my site. Users can post and
in Yii they use this code: defined('YII_DEBUG') or define('YII_DEBUG',true); i've never seen anyone write

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.