yii2架構 操作 mongodb 資料庫執行個體教程

來源:互聯網
上載者:User

一:安裝yii2  關於安裝yii2我們就不??鋁耍?故搶??omposer安裝下載,我們可以安裝basic

模版

composer global require "fxp/composer-asset-plugin:~1.0.3"
 composer create-project --prefer-dist yiisoft/yii2-app-basic basic

或者是advanced

composer global require "fxp/composer-asset-plugin:~1.0.3"
 composer create-project --prefer-dist yiisoft/yii2-app-advanced yii-application

安裝過程需要輸入github密鑰,按照提示操作即可

二:yii2 soft 內建mongodb,安裝追加composer

composer require --prefer-dist yiisoft/yii2-mongodb

/vendor/yiisoft/yii2-mongodb引入了mongodb

三:使用方法crud

config/web.php

'mongodb' => [
'class' => '\yii\mongodb\Connection',
'dsn' => 'mongodb://tank:test@192.168.33.10:27017/admin',
],

use yii\mongodb\Query;
 
public function actionIndex()
{
 
$query = new Query;
// compose the query
$query->select(['_id', 'sex'])
->from('admin')
->limit(10);
// execute the query
$rows = $query->all();
 
print_r($rows);
 
// return $this->render('index');
}



Yii架構中使用mongodb擴充

前提條件:安裝了mongodb資料庫

              安裝了mongo的php驅動

下載Yii的mongo擴充:

這是YiiMongoDbSuite的1.3.6版本
支援PHP Mongo驅動的版本為1.0.5及以下

下載連結:http://pan.baidu.com/s/1jGuWP1O

其它版本下載連結:https://github.com/canni/YiiMongoDbSuite

得到檔案:YiiMongoDbSuite.tar.gz

解包,並將該檔案放至應用的/protected/extensions檔案夾下面

確保檔案夾名稱為:YiiMongoDbSuite

配置應用

vi /protected/config/main.php



 'import' => array(  
      ...  
      'ext.YiiMongoDbSuite.*',    // 外掛程式根目錄檔案  
    ),

    'components' => array(  
      ...  
      'mongodb' => array(  
        'class'            => 'EMongoDB', //主檔案  
        'connectionString' => 'mongodb://127.0.0.1:27017', //伺服器位址
        'dbName'           => 'myDatabaseName',//資料庫名稱  
        'fsyncFlag'        => true, //mongodb的確保所有寫入到資料庫的安全儲存到磁碟  
        'safeFlag'         => true, //mongodb的等待檢索的所有寫操作的狀態,並檢查  
        'useCursor'        => false, //設定為true,將啟用遊標  
      ),  
    ),

這樣就配置好了,當然,請確保你的mongodb安裝在本地,並且連接埠號碼是27107上面的配置才能串連得上。

下面測試一下Model:

將以下代碼放至/protected/models/User.php裡

class User extends EMongoDocument
{
  public $login;
  public $name;
  public $pass;

  // This has to be defined in every model, this is same as with standard Yii ActiveRecord
  public static function model($className=__CLASS__)
  {
    return parent::model($className);
  }

  // This method is required!
  public function getCollectionName()
  {
    return 'users';
  }

  public function rules()
  {
    return array(
      array('login, pass', 'required'),
      array('login, pass', 'length', 'max' => 20),
      array('name', 'length', 'max' => 255),
    );
  }

  public function attributeLabels()
  {
    return array(
      'login'  => 'User Login',
      'name'   => 'Full name',
      'pass'   => 'Password',
    );
  }
}

在控制器裡測試一下

$users = User::model()->findAll();
var_dump($users);

如果沒有報錯,那說明可以正常使用了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.