The PHP yii framework uses MongoDB

Source: Internet
Author: User
Tags dsn yii

1. Installation

Run

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. Configuration

Main.php added.

return [ //.... ' components ' = [ ' mongodb ' + = ' class ' = ' \yii\mongodb\connection ', ' dsn ' = ' + ' mong Odb://developer:[email Protected]:27017/mydatabase ',],],];

For example:

' MongoDB ' = [
' Class ' = ' \yii\mongodb\connection ',
' DSN ' = ' mongodb://127.0.0.1:27017/local ',
],

3, the entity class writing

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 ' =:d eleted 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. Controller Writing

<?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 ' = = ' wrong request type ']);
}

if (!isset ($_post[' data ')) {
return Json_encode ([' E ' =>1002, ' m ' = ' = ' missing necessary parameters ']);
}

$rowJson = $_post[' data '];
$reqData = Json_decode ($rowJson, true);

$messageId = 0;
if (isset ($reqData [' MessageID '])) {
$messageId = $reqData [' MessageID '];
}
$from = 0;
if (isset ($reqData [')]) {
$from = intval ($reqData [' from ']);
}
$to = 0;
if (isset ($reqData [')]) {
$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 ' = ' + ' Add success! ‘]);

}
}

The PHP yii framework uses MongoDB

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.