let me explain my problem, hope you get an idea of what it is.
I have a web service which hide from public access, and have designed a secure way of mysql sql querying using to the service across the websites. so i dont think i can really use the current model layer of Yii2, and that also means i hardly can use activeDataProvider
as no database present.
Currently what i do is to write raw sqls and get all the results and then feed into dataprovider using ArrayDataProvider
.
e.g.
$sql="select * from a_table";
$result=$remote->select($sql);
$dataProvider = new ArrayDataProvider([
'allModels' => $result,
'sort' => [
'attributes' => ['date', 'name'],
],
'pagination' => [
'pageSize' => 10,
],
]);
return $this->render('index', [
'dataProvider' => $dataProvider,
]);
that pose a problem, everytime i need to query the full table. This is not idea if the table is very large. It is better to query in the size of 10 something, however if i do
$sql="select * from a_table LIMIT 10";
no pagination will appear in my case...How do i solve this problem? And if this is not an idea way to talk to external data services, what is ur suggestion?
The pagination won't appear because you only feed 10 rows to the ArrayDataProvider and it has no way of knowing there is more.
If you want to use DataProvider
with remote fetching I would advise you to create your own MyRemoteDataProvider
class by extending BaseDataProvider
and overriding at least these four methods:
use yii\data\BaseDataProvider;
class MyRemoteDataProvider extends BaseDataProvider
{
protected function prepareModels()
{
}
protected function prepareKeys($models)
{
}
protected function prepareTotalCount()
{
}
protected function sortModels($models, $sort)
{
}
}
.. and then of course use your MyRemoteDataProvider
instead of ArrayDataProvider
. If you don't know what the methods should return, please read documentation for BaseDataProvider or get inspired by ArrayDataProvider implementation.