Display records from another module

Hello all,

I am just starting with Yii application development and stuck with an issue for which I couldn’t find any solution. Here is my case:

I have two entities in the application which are created as two modules in Yii: Organization and Contact.

The Organization can have any number of Contacts and it’s linked through the field org_id in Contacts table. (I didn’t used Foreign Key relationships to avoid complexity and this application needs to be deployed in a MyISAM database.)

One of the requirements in the application is that in the Organization VIEW page,the user should be able to see a list of contacts(with pagination) associated with the Organization. The user should be able to get into the Contact view/edit from the Organization View page.

I implemented this in the following fashion:

In modules/organization/controllers/DefaultController.php


 public function actionView($id)

    {

        $this->render('view',array(

            'model'=> Organization::get($id),

            'contactProvider' => Contact::getContactsByOrg($id),

        ));

    }

In modules/organization/views/default/view.php


<?php $this->widget('zii.widgets.grid.CGridView', array(

    'id'=>'users-grid',

    'dataProvider'=>$contactProvider,

    'columns'=>array(

        'contact_name',

        'org_name',

        'category_name',

        'status',

        'modified_by_name',

        'modified_time',

        array(

            'class'=>'CButtonColumn',

        ),

    ),

)); ?>

However, the links generated for the paging, viewing and editing Contacts actually points to Organization instead of Contact. As a result, sorting and paging doesn’t work properly. How can I change the URL in the Contact list?

Hi libregeek, welcome to the forum.

  1. For the buttons, you have to specify CButtonColumn::viewButtonUrl, CButtonColumn::editButtonUrl and CButtonColumn::deleteButtonUrl manually, because their default values don’t fit your situation.

Maybe something like this:




        array(

            'class'=>'CButtonColumn',

            'viewButtonUrl'=>'array("/contact/default/view", "id"=>$data->id)',

            ...

        ),



  1. For paging and sorting, the grid should work fine if your Contact::getContactsByOrg() returns a valid CActiveDataProvider as you see in search() function in the model.

@softark

Thank you very much for the quick response. The button URL worked perfectly.

BTW, how can I verify whether getContactsByOrg() is returning a valid CActiveDataProvider? The paging URL is still pointing to organization instead of contact.

thanks

The pager of a gridview will show the link buttons with URLs like "/current/controller/action?Model_page=2". In your case it should be something like "http://your.domain/organization/default/view?Contact_page=2".

If you are referring to this, it’s not a problem. It doesn’t have to (or, should not) point to “http://your.domain/contact/…”.

@softark

It’s working now. The URL in pagination is app?r=organization/default/view&id=15&ajax=contacts-grid&Contact_page=2 (url truncated due to forum restrictions).

The widgets in Yii is really powerful and it saved several hours for me.

thanks you very much for your timely help.

regards