博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php yii框架使用MongoDb
阅读量:5118 次
发布时间:2019-06-13

本文共 4277 字,大约阅读时间需要 14 分钟。

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'=>'添加成功!']);

}

}

转载于:https://www.cnblogs.com/yuanxiaoping_21cn_com/p/5382359.html

你可能感兴趣的文章
浅谈 unix, linux, ios, android 区别和联系
查看>>
51nod 1428 活动安排问题 (贪心+优先队列)
查看>>
中国烧鹅系列:利用烧鹅自动执行SD卡上的自定义程序(含视频)
查看>>
Solaris11修改主机名
查看>>
latex for wordpress(一)
查看>>
如何在maven工程中加载oracle驱动
查看>>
Flask 系列之 SQLAlchemy
查看>>
aboutMe
查看>>
【Debug】IAR在线调试时报错,Warning: Stack pointer is setup to incorrect alignmentStack,芯片使用STM32F103ZET6...
查看>>
一句话说清分布式锁,进程锁,线程锁
查看>>
python常用函数
查看>>
FastDFS使用
查看>>
服务器解析请求的基本原理
查看>>
[HDU3683 Gomoku]
查看>>
【工具相关】iOS-Reveal的使用
查看>>
数据库3
查看>>
存储分类
查看>>
下一代操作系统与软件
查看>>
【iOS越狱开发】如何将应用打包成.ipa文件
查看>>
[NOIP2013提高组] CODEVS 3287 火车运输(MST+LCA)
查看>>