1、安装
运行
php composer.phar require --prefer-dist yiisoft/yii2-mongodb
or add
"yiisoft/yii2-mongodb": "~2.0.0"
to the require section of your composer.json.
2、配置
main.php里加入
return [ //.... 'components' => [ 'mongodb' => [ 'class' => '\yii\mongodb\Connection', 'dsn' => 'mongodb://developer:password@localhost:27017/mydatabase', ], ], ];
例如:
'mongodb' => [
'class' => '\yii\mongodb\Connection', 'dsn' => 'mongodb://127.0.0.1:27017/local', ],
3、实体类编写
ChatMsg.php
<?php
namespace common\models;
use Yii;
use yii\behaviors\TimestampBehavior;class ChatMsg extends \yii\mongodb\file\ActiveRecord
{ /*public static function find() { return parent::find()->where(['deleted' => false]); }*///public static function find()
//{ // use CustomerQuery instead of the default ActiveQuery //return new CustomerQuery(get_called_class()); //} public function attributes() { return array_merge( parent::attributes(), [ 'content', 'messageId', 'from', 'to', 'conversationId', 'timestamp', 'imageUrl', 'imageSize', 'imageWidth', 'imageHeight', 'type'] ); }}
// Use andWhere()/orWhere() to apply the default condition
// SELECT FROM customer WHERE `deleted`=:deleted AND age>30//$customers = ChatMsg::find()->andWhere('age>30')->all();// Use where() to ignore the default condition
// SELECT FROM customer WHERE age>30//$customers = ChatMsg::find()->where('age>30')->all();
4、控制器编写
<?php
namespace frontend\controllers;use common\helper\MyHttpBasicAuth;use common\helper\UtilHelper;use common\models\ChatMsg;use yii\filters\Cors;use yii\helpers\ArrayHelper;use yii\web\BadRequestHttpException;use yii\web\Controller;use yii\filters\auth\HttpBasicAuth;use yii\caching\DbDependency;use yii\mongodb\Query;class ChatController extends Controller
{ public $enableCsrfValidation = false; public function behaviors() {$behaviors = parent::behaviors();
$behaviors['authenticator'] = [ 'class' => MyHttpBasicAuth::className(), 'except'=>['createmessage', 'getmessages'], ];$behaviors = ArrayHelper::merge([
[ 'class' => Cors::className(), ], ], $behaviors);return $behaviors;
}public function actionGetmessages() {
$query = new Query; // compose the query $query->select(['content', 'messageId','from','to','conversationId','timestamp', 'imageUrl','imageSize','imageWidth','imageHeight','type']) ->from('aaa.files') ->limit(10); // execute the query //$rows = $query->all(); $rows = ChatMsg::find()->asArray()->all(); $rt['e'] = 0; $rt['datas'] = $rows; return json_encode($rt);}
public function actionCreatemessage() {
$request = \Yii::$app->request; if (!$request->isPost) { return json_encode(['e'=>1001, 'm'=>'错误的请求类型']); }if (!isset($_POST['data'])) {
return json_encode(['e'=>1002, 'm'=>'缺少必要参数']); }$rowJson = $_POST['data'];
$reqData = json_decode($rowJson, true);$messageId = 0;
if (isset($reqData['messageid'])) { $messageId = $reqData['messageid']; } $from = 0; if (isset($reqData['from'])) { $from = intval($reqData['from']); } $to = 0; if (isset($reqData['to'])) { $to = intval($reqData['to']); } $imageWidth = 0; if (isset($reqData['imagewidth'])) { $imageWidth = intval($reqData['imagewidth']); } $imageHeight = 0; if (isset($reqData['imageheight'])) { $imageHeight = intval($reqData['imageheight']); } $imageSize = 0; if (isset($reqData['imagesize'])) { $imageSize = intval($reqData['imagesize']); }$conversationId = 0;
if (isset($reqData['conversationid'])) { $conversationId = $reqData['conversationid']; } $timestamp = 0; if (isset($reqData['timestamp'])) { $timestamp = intval($reqData['timestamp']); } $type = 0; if (isset($reqData['type'])) { $type = intval($reqData['type']); } $content = ''; if (isset($reqData['content'])) { $content = $reqData['content']; } $imageUrl = ''; if (isset($reqData['imageurl'])) { $imageUrl = $reqData['imageurl']; }$msg = new ChatMsg();
$msg->imageUrl = $imageUrl; $msg->imageWidth = $imageWidth; $msg->imageHeight = $imageHeight; $msg->imageSize = $imageSize; $msg->content = $content; $msg->type = $type; $msg->timestamp = $timestamp; $msg->conversationId = $conversationId; $msg->to = $to; $msg->from = $from; $msg->messageId = $messageId;$msg->save();
if (!$msg->save()) { return json_encode(['e'=>1004]); }return json_encode(['e'=>0, 'm'=>'添加成功!']);
}
}