yii2中我们可以使用mongo非关系数据库操作,你那么能不能像使用数据库那样来使用orm操作呢?当然可以的。

下面我们来安装mongo:

1.安装mongo:

composer require --prefer-dist yiisoft/yii2-mongodb

www@www:var/www/app$ composer require --prefer-dist yiisoft/yii2-mongodb -
Running 2.2.9 (2022-03-15 22:13:37) with PHP 7.4.28 on Linux / 4.4.0-210-generic
Reading ./composer.json (/htdocs/gitwork/zzoms2-2/composer.json)
Loading config file /home/www/.config/composer/config.json
Loading config file /home/www/.config/composer/auth.json
Loading config file ./composer.json (/htdocs/gitwork/zzoms2-2/composer.json)
Checked CA file /etc/pki/tls/certs/ca-bundle.crt does not exist or it is not a file.
Checked directory /etc/pki/tls/certs/ca-bundle.crt does not exist or it is not a directory.
Checked CA file /etc/ssl/certs/ca-certificates.crt: valid
Executing command (/htdocs/gitwork/zzoms2-2): git branch -a --no-color --no-abbrev -v
Failed to initialize global composer: Composer could not find the config file: /home/www/.c.json

Reading var/www/app/vendor/composer/installed.json
Skipped loading "yiisoft/yii2-composer" as it is not in config.allow-plugins
Reading /home/www/.cache/composer/repo/https---asset-packagist.org/packages.json from cache
Downloading https://asset-packagist.org/packages.json if modified
[200] https://asset-packagist.org/packages.json
Writing /home/www/.cache/composer/repo/https---asset-packagist.org/packages.json into cache
Downloading https://asset-packagist.org/p/provider-latest/3da8cb9075490fd8d39e2ab6304769a84f0f39b2e.json

安装完成后,我们来加配置:

2.在main-local文件里面加上gii地址:

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = [
        'class' => 'yii\gii\Module',
        'generators' => [
            'mongoDbModel' => [
                'class' => 'yii\mongodb\gii\model\Generator'
            ],
        ],
        'allowedIPs' => ['127.0.0.1', '192.168.0.*']
    ];

3.添加mongo 连接配置:

main-local文件里面加上mongo配置:

'mongodb' => [
       'class' => '\yii\mongodb\Connection',
       'dsn' => 'mongodb://127.0.0.1:27017/app',
],
'mongodbshop' => [
      'class' => '\yii\mongodb\Connection',
      'dsn' => 'mongodb://127.0.0.1:27017/shop',
],

如果有多个mongo可以按照上面的写法。

接下来我们要通过gii生成mongo模型:

 mongo有下面的字段:

 生成的模型如下:

<?php

namespace app\models;

use Yii;

/**
 * This is the model class for collection "file".
 *
 * @property \MongoDB\BSON\ObjectID|string $_id
 * @property mixed $attchment_type
 * @property mixed $original_file_name
 * @property mixed $file_name
 * @property mixed $file_path
 */
class File extends \yii\mongodb\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function collectionName()
    {
        return ['app', 'file'];
    }

    /**
     * {@inheritdoc}
     */
    public function attributes()
    {
        return [
            '_id',
            'attchment_type',
            'original_file_name',
            'file_name',
            'file_path',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['attchment_type', 'original_file_name', 'file_name', 'file_path'], 'safe']
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            '_id' => 'ID',
            'attchment_type' => 'Attchment Type',
            'original_file_name' => 'Original File Name',
            'file_name' => 'File Name',
            'file_path' => 'File Path',
        ];
    }
}

这样就可以直接查询了:

$file =  File::find()->where(['file_name'=>'app'])->asArray()->one()

这样就可以使用mongo来操作数据了

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐