qiuxis 2016-10-24 22:48:14 5321次浏览 0条评论 1 0 0

安装好了YII2 的 mongodb 如何去使用,毕竟 ActiveRecord 使用习惯了,在YII2 里面还是可以继续使用mongodb 的 ActiveRecord 。
只不过配置和使用MYSQL 有点不同而已啦,具体就照着以下操作吧。

/frontend/controllers/SiteController.php

<?php 

...
    public function actionIndex()
    {

        $id = 'sssxxxxx';
//        $collection = Yii::$app->mongodb->getCollection('customer');
//        $collection->insert([
//            '_id' => 'sssxxxxx',
//            'status' => 1,
//        ]);
 
        $customer = Customer::findOne(['_id' => $id,]);

        var_dump($customer,2);
 
        return $this->render('index');
    }
	
...

创建:
/frontend/models/Customer.php

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/10/24
 * Time: 21:20
 */

namespace frontend\models;

use yii\mongodb\ActiveRecord;

class Customer extends ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function collectionName()
    {
        return 'customer';
    }
    public function attributes()
    {
        return [
            '_id',
            'status'
        ];
    }

}

到这里其实就不用再详细说明了,直接使用 ActiveRecord 相关方法呗

    没有找到数据。
您需要登录后才可以评论。登录 | 立即注册