zoukankan      html  css  js  c++  java
  • php yii框架使用MongoDb

    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' => 'yiimongodbConnection', 'dsn' => 'mongodb://developer:password@localhost:27017/mydatabase', ], ], ];

    例如:

    'mongodb' => [
    'class' => 'yiimongodbConnection',
    'dsn' => 'mongodb://127.0.0.1:27017/local',
    ],

    3、实体类编写

    ChatMsg.php

    <?php

    namespace commonmodels;

    use Yii;
    use yiiehaviorsTimestampBehavior;

    class ChatMsg extends yiimongodbfileActiveRecord
    {
    /*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 frontendcontrollers;
    use commonhelperMyHttpBasicAuth;
    use commonhelperUtilHelper;
    use commonmodelsChatMsg;
    use yiifiltersCors;
    use yiihelpersArrayHelper;
    use yiiwebBadRequestHttpException;
    use yiiwebController;
    use yiifiltersauthHttpBasicAuth;
    use yiicachingDbDependency;
    use yiimongodbQuery;

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

    }
    }

  • 相关阅读:
    七种数据类型
    js字符串解析成数字
    html节点操作与事件
    form表单的默认提交行为
    CSS中的各种width(宽度)
    Javascript读写CSS属性
    ECMAScript6新特性之Reflect
    ECMAScript6新特性之String API
    ECMAScript6新特性之Array API
    判断Javascript对象是否为空
  • 原文地址:https://www.cnblogs.com/yuanxiaoping_21cn_com/p/5382359.html
Copyright © 2011-2022 走看看