阿江 2017-10-08 17:42:36 2503次浏览 0条回复 0 0 0

说明

学习Yii Framework 2(易2框架)的过程是漫长的,也是充满乐趣的,以下是我学习Yii2框架时的代码实现,提供了较完整的代码,供你参考。不妥之处,请多多指正!

本文主题:实例,一个页面中连接两种数据库(Mysql和MongoDB)

//twodb//2db//使用ActiveRecord D:\phpwork\basic\controllers\CountryController.php

	public function actionTwodb() {
        $dataMysql = CountryMysql::find()
            ->limit(10)
            ->all();
        $dataMongodb =Country::find()
            ->limit(8)
            ->asArray()
            ->all();
        return $this->render('twodb', ['dataMysql' => $dataMysql,'dataMongodb' => $dataMongodb]);
    }

D:\phpwork\basic\models\Country.php

<?php
namespace app\models;
use Yii;
class Country extends \yii\mongodb\ActiveRecord {
    public static function collectionName(){
        return 'country';
    }
    public static function getDb(){
        //显式指定要使用的链接
        return \Yii::$app->mongodb;
    }
    public function attributes() {
        return ['_id', 'code', 'name', 'continent', 'population', 'demo', 'area',];
    }
    public function rules() {
        return [
            [['code', 'name', 'continent', 'population'], 'required'],
            //[['population'], 'integer'],
            [['code','name'], 'unique'],
            ['population', 'double','min'=>10],
            [['demo', 'area'], 'safe'],
            [['code'], 'string', 'max' => 2],
            [['name'], 'string', 'max' => 52],//            [['_id'], 'safe'],//此句多余
        ];
    }
    public function attributeLabels() {
        return ['code' => Yii::t('app', 'Code'), 'name' => Yii::t('app', 'Name'), 'population' => Yii::t('app', 'Population'), 'shoudu.cname' => Yii::t('app', 'City Name(shoudu)'), 'shoudu.populs' => Yii::t('app', 'Population(shoudu)'), 'shoudu[cname]' => Yii::t('app', 'City Name(shoudu)'), 'shoudu[populs]' => Yii::t('app', 'Population(shoudu)'),];
    }
}

D:\phpwork\basic\models\CountryMysql.php

<?php
namespace app\models;
use Yii;
class CountryMysql extends \yii\db\ActiveRecord {
    public static function tableName(){
        return 'country';
    }
    public static function getDb(){
		//显示式指定要使用的链接
        return \Yii::$app->db;
    }
	public function attributes() {
        return ['code', 'name', 'population'];
    }
}

D:\phpwork\basic\config\web.php

return [
	......
    'components' => [
		......
		'db' => [
			'class' => 'yii\db\Connection',
			'dsn' => 'mysql:host=localhost;dbname=yiibasic',
			'username' => 'yiiuser',
			'password' => 'jkljl23asdf23oickxvzklqwer',
			'charset' => 'utf8',
		],
        'mongodb' => [
            'class' => 'yii\mongodb\Connection',
            'dsn' => 'mongodb://dev:123456@127.0.0.1:27017/admin',
        ],
	]
]

D:\phpwork\basic\views\country\twodb.php

<?php
use yii\helpers\ArrayHelper;
$this->title = Yii::t('app', 'Countries');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="country-index">
    Mysql数据:<br>
    <?php
    if($dataMysql){
        echo "<table class='table table-bordered'><tr>";
        echo "<th>#</th>";
        echo "<th>index</th>";
        echo "<th>Name</th>";
        echo "<th>Population</th>";
        echo "<th>Code</th>";
        echo "</tr>";
        $i=1;
        foreach($dataMysql as $k=>$v){
            echo "<tr>";
            echo "<td>".$i."</td>";
            echo "<td>".$k."</td>";
            echo "<td>".ArrayHelper::getValue($v,'name')."</td>";
            echo "<td>".ArrayHelper::getValue($v,'population')."</td>";
            echo "<td>".ArrayHelper::getValue($v,'code')."</td>";
            echo "</tr>";
            $i++;
        }
        echo "</table>";
    }else{
        echo "no record";
    }
    ?>
</div>
<div class="country-index">
    Mongodb数据:<br>
    <?php
    if($dataMongodb){
        echo "<table class='table table-bordered'><tr>";
        echo "<th>#</th>";
        echo "<th>index</th>";
        echo "<th>Name</th>";
        echo "<th>Population</th>";
        echo "<th>Continent</th>";
        echo "<th>Code</th>";
        echo "</tr>";
        $i=1;
        foreach($dataMongodb as $k=>$v){
            echo "<tr>";
            echo "<td>".$i."</td>";
            echo "<td>".$k."</td>";
            echo "<td>".ArrayHelper::getValue($v,'name')."</td>";
            echo "<td>".ArrayHelper::getValue($v,'population')."</td>";
            echo "<td>".ArrayHelper::getValue($v,'continent')."</td>";
            echo "<td>".ArrayHelper::getValue($v,'code')."</td>";
            echo "</tr>";
            $i++;
        }
        echo "</table>";
    }else{
        echo "no record";
    }
    ?>
</div>
测试结果:
/*
Mysql数据:
#	index	Name	Population	Code
1	0	Australia	18886000	AU
2	1	Brazil	170115000	BR
3	2	Canada	1147000	CA
4	3	China	1277558000	CN
5	4	Germany	82164700	DE
6	5	France	59225700	FR
7	6	United Kingdom	59623400	GB
8	7	India	1013662000	IN
9	8	Russia	146934000	RU
10	9	United States	278357000	US
Mongodb数据:
#	index	Name	Population	Continent	Code
1	0	Laowo	196	Europe	CA
2	1	Korea	124234	Asia	23
3	2	China	1277558000	Asia	CN
4	3	Germany	234234	Asia	42
5	4	Canada	170115000	Asia	BR
6	5	Mengo	78	Europe	C1
7	6	Nipor	88	Asia	C2
8	7	America	1147000	Asia	C3
*/

(全文完)

    没有找到数据。
您需要登录后才可以回复。登录 | 立即注册