Union two table in Dataprovider

I have two tables Group and User. They have a Many-to-many relationships.

Somehow my application need to assign records to Group or User.

So, I need to make a dropdownlist using dataprovider. The dropdownlist should contains all Groups and Users. How can I do that? Many. thanks.

Currently, I just created a Assigned to Model and merge by myself, which I think is a little bit stupid. :unsure: :rolleyes:




<?php


class AssignedToModel

{


  public static function model($className=__CLASS__) {

    return new $className();

  }


  public function findAll()

  {

    $assigned_to = array();

    $users = UserModel::model()->findAll();

    $groups = GroupModel::model()->findAll();

    foreach ($users as $user)

    {

      $assigned_to[] = array(

        'assigned_to_id'=>$user->userid,

        'name'=>$user->username);

    }

    foreach ($groups as $group)

    {

      $assigned_to[] = array(

        'assigned_to_id'=>$group->groupid,

        'name'=>$group->groupname);

    }

  }

}




you can create view in database that unions those tables and then create model that points to that view.

there is however one problem with primary key duplicates - what if you have user with pk=1 and group with pk=1? how would you distinguish them in such union?

@redguy,

slightly of the op topic, but could you advice on the performance of database’s view as a model compare to a model from a table?

I encountered bad experience with the model rendered in admin view (CGridView) was really slow to load. especially, when you have 50/100 rows in the CGridView.

There could be all sorts of reasons why your cgridview page takes a long time to load. You could be loading too much data, or querying too many tables. Analyzing yii’s web logs can be helpful in determining what’s taking up too much processing time in your code.