asialong

asialong

这家伙有点懒,还没写个性签名!

  • 财富值6065
  • 威望值10
  • 总积分10555

个人信息

  • 回复了 的回复
    
    $dsn = 'select from config db table';
    $db = new Connection($dsn);
    $provider = new ActiveDataProvider([
           'db' => $db,
           'query' => Post::find(),
           'pagination' => [
              'pageSize' => 20,
           ],
    ]);
    

    大概类似这样,你可以通过数据库配置表构造远程数据库的$dsn,然后构造一个Connection对象连接远程数据库,然后,将这个对象传给DataProvider即可。

    $apiweb = new ApiWeb;

        $connection = $apiweb->getDatabase($webid);
        
        $query = (new Query())->from('cm_category');   //这个地方可以用Post::find() 这样的方式来读取远程表吗?
        $dataProvider = new ActiveDataProvider([
            'db' => $connection,
            'query' => $query,
            'pagination' => [
                'pageSize' => 20,
            ]
        ]);
    
  • 回复了 的回复
    
    $dsn = 'select from config db table';
    $db = new Connection($dsn);
    $provider = new ActiveDataProvider([
           'db' => $db,
           'query' => Post::find(),
           'pagination' => [
              'pageSize' => 20,
           ],
    ]);
    

    大概类似这样,你可以通过数据库配置表构造远程数据库的$dsn,然后构造一个Connection对象连接远程数据库,然后,将这个对象传给DataProvider即可。

    'query' => Post::find(), 这个地方我想就是读远程数据库的表,这样写的话,会提示,The table does not exist: ........ 就是该表不存在

  • 发布了话题
    yii2 跨库操作问题
  • 提出了问题
    yii2 跨库操作问题
  • 回复了 的回复

    可以类似这样处理。
    main-local.php:

    'components' => [
            'db' => [
                'class' => 'yii\db\Connection',
                'dsn' => 'mysql:host=192.168.80.1;dbname=test1;port=3306',
                'username' => 'root',
                'password' => '123456',
                'charset' => 'utf8',
            ],
    		'db2' => [
    			'class' => 'yii\db\Connection',
                'dsn' => 'mysql:host=192.168.80.1;dbname=test2;port=3306',
                'username' => 'root',
                'password' => '123456',
                'charset' => 'utf8',
    		],
    

    Model:

    $connection = \Yii::$app->get ( "db" );
    或
    $connection = \Yii::$app->get ( "db2" );
    

    大侠请指导一下啊

  • 回复了 的回复

    可以类似这样处理。
    main-local.php:

    'components' => [
            'db' => [
                'class' => 'yii\db\Connection',
                'dsn' => 'mysql:host=192.168.80.1;dbname=test1;port=3306',
                'username' => 'root',
                'password' => '123456',
                'charset' => 'utf8',
            ],
    		'db2' => [
    			'class' => 'yii\db\Connection',
                'dsn' => 'mysql:host=192.168.80.1;dbname=test2;port=3306',
                'username' => 'root',
                'password' => '123456',
                'charset' => 'utf8',
    		],
    

    Model:

    $connection = \Yii::$app->get ( "db" );
    或
    $connection = \Yii::$app->get ( "db2" );
    

    请问是怎么设置的啊???

  • 2015-12-22 已签到
    连续签到4天,获得了20个金钱
  • 2015-12-21 已签到
    连续签到3天,获得了15个金钱
  • 2015-12-20 已签到
    连续签到2天,获得了10个金钱
  • 2015-12-19 已签到
    连续签到1天,获得了5个金钱
10555/20000
资料完整度
10/100
用户活跃度
0/100

Ta的关注

24

Ta的粉丝

8

Ta的访客

42