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

本文共 4212 字,大约阅读时间需要 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'=>'添加成功!']);

}

}

转载地址:http://yiexx.baihongyu.com/

你可能感兴趣的文章
去 IOE,MySQL 完胜 PostgreSQL
查看>>
++i 和 i++ 性能上的区别
查看>>
Mysql运维管理-一主多从宕机从库切换主库继续和从库同步过程16
查看>>
Redis的RDB AOF DATABASE
查看>>
linux下启动某个进程
查看>>
Tomcat优化之配置NIO运行模式
查看>>
安装zabbix
查看>>
用XSLT和XML改进Struts
查看>>
WEB测试—功能测试
查看>>
在react或vue中,for循环用Index作为key值是好还是坏呢?
查看>>
2014.10.1 Form中显示pdf文件
查看>>
那些闪亮的日子之二
查看>>
PL/SQL Developer连接本地Oracle 11g 64位数据库
查看>>
NERDTree 快捷键辑录
查看>>
Python数据分析Numpy库方法简介(一)
查看>>
javaWeb:相关监听方法汇总
查看>>
JSP 实现 之 读取数据库显示图片
查看>>
JS——特效秀
查看>>
【mybatis】mybatis使用java实体中定义的常量,或静态方法
查看>>
Beta冲刺——day6
查看>>