I have two data sources

1) Database
2) Memcached or Totally different database

From 1st database I am getting the group members list IDs ( has more than 10 thousand rows) and after getting the list I query second database or hit Memcached to get the actual details

$connection = Yii::app()->db; 
$sql = 'select user_id,joined_date from group_data where group_id=:group_id ';

$dataProvider = new CSqlDataProvider($sql, array(
                    'keyField' => 'user_id',
                    'sort' => array(
                        'attributes' => array(
                            'user_id',
                            'joined_date'
                        ),
                        'defaultOrder'=>array(
                         'user_id ASC',
                        )
                    ),
                    'pagination' => array(
                        'pageSize' => 30,
                    ),
                    'totalItemCount' => $count,
                ));


$connection_master = Yii::app()->masterdb; 

In the above data provider , I am not sure how to include my second database and get the actual user name since it is in other database .No direct connection from one database to other.

Here the problem is pagination is based on the first database table ( 30 records per page) and the actual data is from second table .

Any idea on how to implement this ?

Thank You

有帮助吗?

解决方案

You could build your own custom data provider class. You can extend it from CDataProvider and implement the missing abstract methods:

abstract protected function fetchData();
abstract protected function fetchKeys();
abstract protected function calculateTotalItemCount();

Notice, that your storage model already comes very close to these methods: You store the IDs in one database and the actual data in another. So you can focus on one DB at a time in each of these methods.

You'd start with fetchKeys(). There you need to query the keys (=IDs) from your second database. Have a look at CSqlDataProvider::fetchData() to see how to use the CPagination object from $this->getPagination() to find out the current limit and offset. If you need sorting you'd also inspect the CSort object from $this->getSort() for current sort settings. With all that data available you should be able to build the query for the IDs on the currently requested result page.

Then in fetchData() you can obtain those keys through $this->getKeys(). You should not call fetchKeys() directly, because with getKeys() the result from fetchKeys() is "cached", so if it's called again later in the same request, there won't be another query. With that keys you now can easily query your main database. The result will represent the rows on the current page.

Finally you have to implement calculateTotalItemCount(). This is very easy again: Just query your second DB for the total number of items.

That's all you need to do - the code in the base CDataProvider class will take care of the rest and call your methods on demand.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top