Home  >  Article  >  Backend Development  >  Problems with thinkphp operating mongodb

Problems with thinkphp operating mongodb

WBOY
WBOYOriginal
2016-08-10 09:07:24960browse

The mongodbdatabase connection has been configured in the framework. How to implement the CURD operation? The following is my operation

<code>$model = M('category');
// $model = D('category');

/** 查询 */
$result = $model->where($where)->select();</code>

My idea is that the method that comes with the framework can be operated in the same way as mysql, but I found that this does not work, and it always says that there is a problem with the instantiation of my M method. But category does exist in my mongodb. Solve

Reply content:

The

mongodbdatabase connection has been configured in the framework. How to implement the CURD operation? The following is my operation

<code>$model = M('category');
// $model = D('category');

/** 查询 */
$result = $model->where($where)->select();</code>
My idea is that the method that comes with the framework can be operated in the same way as

mysql, but I found that this does not work, and it always says that there is a problem with the instantiation of my M method. But category does exist in my mongodb. Solve

I don’t know if my method can solve your problem. I also encountered this kind of problem before and made an error using the

M

method. I forgot the specific error and it may be different from yours. I used the D method and the operation mysql and there was no problem, but using D You need to pay attention to the method. Just define the corresponding class in the model file. For example <pre class="brush:php;toolbar:false">&lt;code&gt;&lt;?php /** * Description: MongoDB操作 * Author: yangxiangming@live.com * Date: 2015/9/9 * Time: 13:35 */ namespace Bbsapi\Model; use Think\Model\MongoModel; class ExampleModel extends MongoModel { }&lt;/code&gt;</pre> The calling operation is as follows

<code><?php
/**
 * Description: MongoDB操作
 * Author: yangxiangming@live.com
 * Date: 2015/9/9
 * Time: 13:51
 */

namespace \Controller;
use Think\Model\ExampleModel;
class ExampleController extends ExampleModel{

    public function example(){
        $where['_id'] = '54dd9116e4b061818991ac7d';
        $model = D('Example');

        /** 查询 */
        $result = $model->where($where)->select();

        /** 添加 */
        $data['name'] = 'Example';
        ……
        $model->add($data);

        /** 更新 */
        $data['name'] = 'ExampleTmp';
        ……
        $model->where($where)->save($data);

        /** 删除 */
        $model->where($where)->delete();
    }
}</code>

Reference link

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn