duanraa1984 2014-11-28 08:28
浏览 35

Yii2外部数据检索困难

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?

  • 写回答

1条回答 默认 最新

  • duanhuo7441 2014-11-30 12:25
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。