User1988
User1988

Reputation: 1997

how to display records in dataprovider of yii?

I want to use this query in dataprovider

select user.username ,company_user.* from user left join company_user on company_user.user_id=user.id where company_user.company_id=".$id

how to write in CActiveDataProvider

plz help me Thank in advance...

I have 3 tables
company_user->id,company_id,user_id,first_name,last_name
company->id,name_of_company
user->id,username,password

I want all records from company_user + username from user

Thanks in advance...:)

i want list in CGridView

in my user model I have written this type of relations

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(
            'company_user'          =>array(self::HAS_ONE,'CompanyUser','user_id','select' =>array('first_name','status'),
                                            'with'=>array('company'=>array(self::BELONGS_TO, 'Company', 'company_id','joinType' => 'INNER JOIN')),
                                    ),
            'company_user_rel_only' =>array(self::HAS_ONE,'CompanyUser','user_id','select' =>array('first_name', 'last_name')),
            
    }

Upvotes: 1

Views: 1826

Answers (1)

Skatox
Skatox

Reputation: 4284

With Yii, you should use the model approach, something like this should work:

$criteria = new CDbCriteria;
$criteria->with = 'user';
$criteria->compare('company_id', $id); //if error due to similar column name change it to t.company_id
$dataProvider =  new CActiveDataProvider('CompanyUser', array(
                     'criteria' => $criteria,
                 ));

I'll asume that relation's name between CompanyUser and User is user, if not, change the with attribute to the correct name. Also, this will give objects of the type CompanyUser, if you wanna access to the user atributes, access them with ->user->username

Upvotes: 2

Related Questions